NoMethodError 尝试使用 Ruby RestClient

NoMethodError trying to use the Ruby RestClient

我正在尝试使用 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.

之后

我会检查 mime-types gem 的哪个版本与您的 RestClient 版本一起安装。看起来版本可能不兼容。快速深入研究 RestClient 源代码,您的堆栈跟踪发生在此处:

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

貌似在安装的mime-types版本中,@extension_index是指没有[]实例方法的集合。 mime 类型 gem 有点难以深入了解,但我认为 Set 正在 /lib/mime/types/container.rb (here).

中初始化

因此,此时这意味着您有两个选择。要么将 mime 类型 gem 固定到 Gemfile 中的旧版本,要么升级 RestClient。看起来 RestClient 版本 1.7.3 删除了导致此问题的猴子补丁。