【问题标题】:NoMethodError trying to use the Ruby RestClientNoMethodError 尝试使用 Ruby RestClient
【发布时间】:2016-04-29 08:45:35
【问题描述】:

我正在尝试使用 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 之后。

【问题讨论】:

  • 我刚刚将这个 gem 添加到我的一个项目中。你能显示你的代码并确认 api 正在返回一些东西吗?
  • 我认为空白哈希的问题 {}.to_json 尝试在其中传递一些值,例如 { 'x' => 1 }.to_json。只是猜测。
  • 您能否更新到更高版本的 RestClient gem?
  • Hola Pablo 你能更新到最新版本吗?你可以在 Rails 之外尝试吗?我已经尝试过使用最新的 gem,它可以工作,即使是空的 {}。来自 Simplificator 的欢呼

标签: ruby rest-client


【解决方案1】:

我会检查您的 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.rb (here) 中初始化。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多