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?