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.

Leo
  • 4,217
  • 4
  • 25
  • 41

3 Answers3

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" 
victorkt
  • 13,992
  • 9
  • 52
  • 51
  • Yep, this is actually copied from a subclass, so `base_uri` is set. I’ll correct it. Thanks! – Leo May 13 '15 at 16:43
  • Ah, I get it! I tried `res.last_uri` but got an error. I see now that including `.response` gets to the right object. – Leo May 13 '15 at 16:44
  • 1
    Works but wont show the parameters passed to the URL. – djangofan Jan 19 '17 at 01:09
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.

DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
  • 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. – Leo May 13 '15 at 16:45
  • I get a `NoMethodError: undefined method 'debug_output' for HTTParty:Module` error. – infused May 13 '15 at 16:47
  • https://ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/HTTP.html#method-i-set_debug_output what about that? – hatenine Nov 15 '16 at 09:10
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
Bruno Degomme
  • 883
  • 10
  • 11