我必须使用提供的外部API中的方法,并且一个方法是带有json主体的GET。我正在通过rails使用rest-client gem进行连接,这可以完成这项工作。
我知道RestClient.post接受json:
RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
而且我的情况也很完美。但是,如果我将.post更改为.get,我会收到此错误:
ArgumentError: wrong number of arguments (3 for 2)
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/rest-client-1.6.7/lib/restclient.rb:67:in `get'
from /Users/toptierlabs/Desktop/Proyectos/AppraisalLane/app/models/autoniq.rb:8:in `getCircleInventory'
from (irb):3
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/toptierlabs/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Rails中有没有办法用json体进行GET?或任何可能的解决方案将“与json身体相关”转换为其他东西?没有必要使用rest-client gem,如果任何人有另一个解决方案也是受欢迎的。
谢谢!
答案 0 :(得分:1)
解决:
RestClient.get“http://example.com/resource”,params:{'json'=&gt; {'x'=&gt; 1} .to_json} ,: content_type =&gt; :json,:accept =&gt; :JSON