3

以下请求有什么问题?

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

这返回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

编辑:

我试过这个:

response = request.response
puts response.body

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

4

2 回答 2

7

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

您需要在响应正文可用之前进行获取。

编辑:这是一个可操作的解决方案。它不使用您的网站,我什至无法手动访问。但是,这会返回响应代码 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
于 2013-09-26T17:13:47.520 回答
0

问题是您实际上并没有执行您的请求。以下代码应该可以工作。

request = Typhoeus::Request.new("http://fluidsurveys.com/api/v2/groups",
                                method: :get,
                                userpwd: "test_user:test_password",
                                headers: { 'ContentType' => "application/json"})

request.run
response = request.response
response_code = response.code
response_body = response.body
于 2016-08-09T22:00:32.707 回答