首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ruby gem Typhoeus的Api请求

使用Ruby gem Typhoeus的Api请求
EN

Stack Overflow用户
提问于 2013-09-26 16:02:20
回答 2查看 6.2K关注 0票数 3

下面的请求有什么问题?

代码语言:javascript
复制
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中很好地工作

代码语言:javascript
复制
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

编辑:

我试过这个:

代码语言:javascript
复制
response = request.response
puts response.body

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-26 17:13:47

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

您需要在响应体可用之前完成get。

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

代码语言:javascript
复制
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
票数 7
EN

Stack Overflow用户

发布于 2016-08-09 22:00:32

问题是你并没有真正执行你的请求。下面的代码应该可以工作。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19033167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档