下面的请求有什么问题?
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)
发布于 2013-09-26 17:13:47
来自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发布于 2016-08-09 22:00:32
问题是你并没有真正执行你的请求。下面的代码应该可以工作。
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.bodyhttps://stackoverflow.com/questions/19033167
复制相似问题