Luigi Luigi - 1 year ago 70
Ruby Question

Api Requests with Ruby gem Typhoeus

What is wrong with the following request?

request ="",
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

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


I tried this:

response = request.response
puts response.body

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

Answer Source


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 ="",
                                  method: :get,
                                  userpwd: "test_user:test_password",
                                  headers: { ContentType: "application/json"})
  response =
  puts response.response_body