尝试使用Ruby RestClient的NoMethodError

时间:2016-04-29 08:45:36

标签: ruby rest-client

我尝试使用RestClient与API通信,我收到此错误并进行回溯:

>> 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>'

任何想法在这里出了什么问题?据我所知,该请求可能甚至没有到达服务器,但我的行正在关注the RestClient documentation

1 个答案:

答案 0 :(得分:2)

我会检查您的RestClient版本安装了mime-types gem的版本。看起来版本可能不兼容。快速挖掘RestClient源代码,您的堆栈跟踪就在这里发生:

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

看起来像安装的mime-types版本,@extension_index指的是没有[]实例方法的集合。 mime-types gem有点难以理解,但我认为Set已经在/lib/mime/types/container.rbhere)初始化了。

所以,在这一点上,这意味着你有两个选择。将mime-types gem固定到Gemfile中的旧版本,或者升级RestClient。看起来像RestClient版本1.7.3取出导致此问题的猴子补丁。