Luigi Luigi - 3 months ago 19
Ruby Question

Api Requests with Ruby gem Typhoeus

What is wrong with the following request?

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


This returns
undefined method body for #<Typhoeus::Request:0x007f8e50d3b1d0> (NoMethodError)


The following request works fine with
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


EDIT:

I tried this:

response = request.response
puts response.body


with no luck. I receive this :
undefined method body for nil:NilClass (NoMethodError)

R_G R_G
Answer

From https://github.com/typhoeus/typhoeus

You need to do the get before the response body is available.

EDIT: Here is an operable solution. It doesn't use your website, which I couldn't access even manually. But, this returns response code 200 and the response_body. Running this in my debugger showed the complete response, which you could see using "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
Comments