How do I inspect the full URL generated by HTTParty?
Asked Answered
B

4

21

I want to look at the full URL the HTTParty gem has constructed from my parameters, either before or after it is submitted, it doesn’t matter.

I would also be happy grabbing this from the response object, but I can’t see a way to do that either.

(Bit of background)

I’m building a wrapper for an API using the HTTParty gem. It’s broadly working, but occasionally I get an unexpected response from the remote site, and I want to dig into why – is it something I’ve sent incorrectly? If so, what? Have I somehow malformed the request? Looking at the raw URL would be good for troubleshooting but I can’t see how.

For example:

HTTParty.get('http://example.com/resource', query: { foo: 'bar' })

Presumably generates:

http://example.com/resource?foo=bar

But how can I check this?

In one instance I did this:

HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3] }

But it didn’t work. Through experimenting I was able to produce this which worked:

HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3].join(',') }

So clearly HTTParty’s default approach to forming the query string didn’t align with the API designers’ preferred format. That’s fine, but it was awkward to figure out exactly what was needed.

Brethren answered 13/5, 2015 at 16:31 Comment(0)
S
32

You didn't pass the base URI in your example, so it wouldn't work.

Correcting that, you can get the entire URL like this:

res = HTTParty.get('http://example.com/resource', query: { foo: 'bar' })
res.request.last_uri.to_s
# => "http://example.com/resource?foo=bar" 

Using a class:

class Example
  include HTTParty
  base_uri 'example.com'

  def resource
    self.class.get("/resource", query: { foo: 'bar' })
  end
end

example = Example.new
res = example.resource
res.request.last_uri.to_s
# => "http://example.com/resource?foo=bar" 
Systematology answered 13/5, 2015 at 16:39 Comment(3)
Yep, this is actually copied from a subclass, so base_uri is set. I’ll correct it. Thanks!Brethren
Ah, I get it! I tried res.last_uri but got an error. I see now that including .response gets to the right object.Brethren
Works but wont show the parameters passed to the URL.Blasphemy
D
12

You can see all of the information of the requests HTTParty sends by first setting:

class Example
  include HTTParty
  debug_output STDOUT
end

Then it will print the request info, including URL, to the console.

Druse answered 13/5, 2015 at 16:40 Comment(3)
This is really useful. I’m going to accept the answer @victorkohl provided because it’s more directly applicable, but as a general approach to debugging this is great.Brethren
I get a NoMethodError: undefined method 'debug_output' for HTTParty:Module error.Rhodonite
ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/… what about that?Ovi
D
0

As explained here, if you need to get the URL before making the request, you can do

HTTParty::Request.new(:get, '/my-resources/1', query: { thing: 3 }).uri.to_s
Dean answered 10/2, 2023 at 18:15 Comment(0)
G
0

This is the one that helped me:

HTTParty::Basement.debug_output $stdout
Guadalajara answered 24/5 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.