使用Ruby gem Typhoeus进行Api请求

Lui*_*igi 3 ruby api httparty typhoeus

以下请求有什么问题?

request = Typhoeus::Request.new("http://fluidsurveys.com/api/v2/groups",
                                    method: :get,
                                    userpwd: "test_user:test_password",
                                    headers: { 'ContentType' => "application/json"})
response = request.body
puts response
Run Code Online (Sandbox Code Playgroud)

这回来了 undefined method body for #<Typhoeus::Request:0x007f8e50d3b1d0> (NoMethodError)

以下请求适用于httparty:

call= "/api/v2/groups/"
auth = {:username => "test_user", :password => "test_password"}
url = HTTParty.get("http://fluidsurveys.com/api/v2/groups",
                   :basic_auth => auth,
                   :headers => { 'ContentType' => 'application/json' } )
response = url.body
puts response
Run Code Online (Sandbox Code Playgroud)

编辑:

我试过这个:

response = request.response
puts response.body
Run Code Online (Sandbox Code Playgroud)

没有运气.我收到了这个:undefined method body for nil:NilClass (NoMethodError)

Ric*_*d_G 7

来自https://github.com/typhoeus/typhoeus

您需要在响应主体可用之前执行get操作.

编辑:这是一个可操作的解决方案.它不使用您的网站,我甚至无法手动访问.但是,这会返回响应代码200和response_body.在我的调试器中运行它显示了完整的响应,您可以使用"puts response.inspect"查看.

class TyphoeusTry
  require 'typhoeus'
  request = Typhoeus::Request.new("http://www.google.com",
                                  method: :get,
                                  userpwd: "test_user:test_password",
                                  headers: { ContentType: "application/json"})
  response = request.run
  puts response.response_body
end
Run Code Online (Sandbox Code Playgroud)