Detect redirect with ruby mechanize

user337620 picture user337620 · Jul 6, 2013 · Viewed 8.9k times · Source

I am using the mechanize/nokogiri gems to parse some random pages. I am having problems with 301/302 redirects. Here is a snippet of the code:

agent = Mechanize.new
page = agent.get('http://example.com/page1')

The test server on mydomain.com will redirect the page1 to page2 with 301/302 status code, therefore I was expecting to have

page.code == "301"

Instead I always get page.code == "200".

My requirements are:

  • I want redirects to be followed (default mechanize behavior, which is good)
  • I want to be able to detect that page was actually redirected

I know that I can see the page1 in agent.history, but that's not reliable. I want the redirect status code also.

How can I achieve this behavior with mechanize?

Answer

pguardiario picture pguardiario · Jul 14, 2013

You could leave redirect off and just keep following the location header:

agent.redirect_ok = false
page = agent.get 'http://www.google.com'
status_code = page.code

while page.code[/30[12]/]
  page = agent.get page.header['location']
end