Pablo Fernandez
Pablo Fernandez

Reputation: 287390

NoMethodError trying to use the Ruby RestClient

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.

Upvotes: 2

Views: 1324

Answers (1)

photoionized
photoionized

Reputation: 5232

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.

Upvotes: 2

Related Questions