2

I'm trying to use RestClient to talk to an API and I'm getting this error and backtrace:

>> RestClient.post "http://localhost:8081/accounts", {}.to_json, content_type: :json, accept: :json
NoMethodError: undefined method `[]' for #<Set: {#<MIME::Type: application/json>}>
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:307:in `type_for_extension'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:312:in `type_for_extension'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:278:in `block in stringify_headers'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `each'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `inject'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `stringify_headers'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:92:in `make_headers'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:58:in `initialize'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
    from (irb):5
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/pupeno/.rvm/gems/ruby-2.3.1@console/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/pupeno/Documents/qredo/console/bin/rails:9:in `require'
    from /Users/pupeno/Documents/qredo/console/bin/rails:9:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Any ideas what's going wrong here? As far as I can see, the request might not be even getting to the server, but my line is following the RestClient documentation.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • I just added this gem to one of my projects. Can you show your code and confirm that the api is returning something? – margo Apr 29 '16 at 08:56
  • I think problem with blank hash {}.to_json try pass some value in it like { 'x' => 1 }.to_json. Just a guess. – Sachin R Apr 29 '16 at 08:58
  • Are you able to update to a later version of the RestClient gem? – Paul Fioravanti Apr 29 '16 at 09:02
  • Hola Pablo Can you update to the latest version? Can you try outside of Rails? I've tried with latest gem and it worked, even with empty {}. Cheers from Simplificator – Pascal Apr 29 '16 at 09:54

1 Answers1

2

I'd check what version of the mime-types gem is installed with your version of RestClient. It looks like the versions may be incompatible. Quickly digging into the RestClient source, your stacktrace is happening here:

https://github.com/rest-client/rest-client/blob/v1.6.7/lib/restclient/request.rb#L307

looks like the in the version of mime-types installed, @extension_index refers to a set which has no [] instance method. The mime-types gem is a bit hard to poke around in, but I think that the Set is getting initialized in /lib/mime/types/container.rb (here).

So, at this point it means you have two options. Either pin the mime-types gem to an older version in your Gemfile, or upgrade RestClient. Looks like RestClient version 1.7.3 took out the monkey patch that's causing this.

photoionized
  • 5,092
  • 20
  • 23