Request URI before making request
Is there a way to get the request URI before performing the request? I see that I can do
res = self.class.get("path", options)
res.request.last_uri.to_s
After the request is made, but I would like to be able to construct that URI before making a request. Is there a way to do this?
What a coincidence.... came here to look exactly for that same method.
I didn't find anything in the code, and I would also love to call something like self.class.url path, params to get the constructed URL without performing any request.
This would be super useful, and I guess for someone familiar with the code, its going to be a one line method.
There isn't an official supported way. What is your use case? I've been thinking about adding instrumentation to httparty that instruments things before/after requests. I wonder if something like that could help you.
Well, I can speak for my use case.
I am an API junkie. I have developed several API wrapper gems, which let you convert domain.com/endpoint?param=value to something like api.endpoint param: value ( example ).
So since this gem has some hidden "magic" to generate the URL for the request, it is often useful to see the URL it generated, for debugging and to better understand how it works. This way, you can paste the URL in the browser, and figure out if its a problem with your request or with something else.
More specifically, you can see this example - it is an API wrapper-building utility based on HTTParty, and its #url method is just inefficient since it makes the request when all that we need is just the URL.
Although before* and after* callbacks would be nice, I do not think it should be instead of a simple #url method that just returns the constructed URL string. My opinion at least.
If you ask me, the #uri method should have the same signature as the #get method, and call something similar to #perform_request only without the .perform.
Or something similar...
Have you tried logging using the curl logger? I usually use then when I'm trying to see what request is actually being made. There is an example of it on this comment. Totally fine if that doesn't help, but thought I would suggest it just in case.
Thanks, but for me the logger is an overkill. The #last_url is good enough for debugging after the fact, just seems like a missing #url method that shows the constructed URL without performing any request.
I agree with @DannyBen - It would be nice to have a method (unless it already exists that I don't know about) to get the url without making the request.
@jnunemaker Sorry I missed your first comment. My use case is that I have certain scenarios where I need to know what the request url will be, but I don't actually want to execute that request. I return that URL in an API response so that a front end application can take over and make the request when it needs to. I realized that might be a unique use case, so I understand if its not a super high priority.
I use httparty for pretty much all my other use cases when making requests, but unfortunately I had to write my own wrapper for this in order to accomplish this requirement.
Thanks for a great tool, If I were a bit more knowledgeable I'd try to create a PR for this, but it might be a bit out of my current skill set.
I think you can get behavior like this by doing:
HTTParty::Request.new(:get, '/my-resources/1', query: { thing: 3 }).uri.to_s
# "/my-resources/1?thing=3"
Closing this, since @Rsullivan00 posted a solution to the original question
HTTParty::Request.new(:get, '/my-resources/1', query: { thing: 3 }).uri.to_s
As help/documentation is scarce on HTTParty::Request.new(), just a hint for future newbies like me, while this expression will return the URI, but it will not execute as a HTTP request. The ':get' is not a valid syntax HTTP method for Request.new() (change it to any gibberish and see, the above code will still run as it just constructs the HTTP request with whatever you put into).
Try this:
HTTParty::Request.new(Net::HTTP::Get, 'https://google.com/search', query: {q: "hoge+fuga+piyo"}).perform