Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby HTTParty - get the redirected URL

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.

like image 301
Rajasree Muraleedharan Avatar asked Sep 23 '15 06:09

Rajasree Muraleedharan


4 Answers

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
like image 126
Arman H Avatar answered Nov 04 '22 00:11

Arman H


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.

like image 4
Aivils Štoss Avatar answered Nov 04 '22 00:11

Aivils Štoss


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'
like image 2
H6. Avatar answered Nov 03 '22 23:11

H6.


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.

like image 1
jef Avatar answered Nov 03 '22 23:11

jef