Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly build http get request in ruby

Tags:

http-get

ruby

I want to request user information from an API using ruby so I'm following this ruby http cheat sheet:

http://augustl.com/blog/2010/ruby_net_http_cheat_sheet/

This is what I'm using so far:

    #!/usr/bin/ruby
    require "net/http"
    require "uri"

    uri = URI.parse("https://cirrus.app47.com/api/users/obfuscated_userID")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(uri.request_uri)
    request.initialize_http_header({"X-Token" => "obfuscated-TokenString"})
    request.initialize_http_header({"Accept" => "application/json"})

    response = http.request(request)
    puts response.to_s

Printing the response gives me #<Net::HTTPBadRequest:0x007fe37cac2da0> so what am I doing wrong to build or execute the request?

like image 606
kraftydevil Avatar asked Sep 04 '26 23:09

kraftydevil


1 Answers

You probably want to call response.body

You can find the documentation for Net::HTTPResponse here

like image 108
davidrac Avatar answered Sep 07 '26 20:09

davidrac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!