I am trying to send an HTTP request using HTTParty with some parameters,
Eg:
GET URL: http://example1.com?a=123&b=456
response_to_get = HTTParty.get('http://example1.com?a=123&b=456')
The redirect_url for this particular request is http://example2.com
When I tried the URL http://example1.com?a=123&b=456
in browser, it redirects to the expected URL with parameter value appended to it like http://example2.com?c=123trt58
, but when I did it using HTTParty, I'm getting an HTML reponse only.
My requirement is to get the URL(http://example2.com?c=123trt58) out from the response.
Thanks in Advance.
If you do not want to follow redirects, but still want to know where the page is redirecting to (useful for web crawlers, for example), you can use response.headers['location']
:
response = HTTParty.get('http://httpstat.us/301', follow_redirects: false)
if response.code >= 300 && response.code < 400
redirect_url = response.headers['location']
end
Slightly corrected @High6 answer:
res = HTTParty.head("example1.com?a=123&b=456")
res.request.last_uri.to_s
This will prevent large response download. Just read headers.
To get the final URL after a redirect using HTTParty
you can use:
res = HTTParty.get("http://example1.com?a=123&b=456")
res.request.last_uri.to_s
# response should be 'http://example2.com?c=123trt58'
require 'uri'
require 'net/http'
url = URI("https://<whatever>")
req = Net::HTTP::Get.new(url)
res = Net::HTTP.start(url.host, url.port, :use_ssl => true) {|http|
http.request(req)
}
res['location']
PS: This is using native ruby HTTP lib.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With