Sabuj Hassan
Sabuj Hassan

Reputation: 39385

How to get request headers using Mechanize?

I want to get the request headers that were sent by Mechanize during the HTTP request:

require 'rubygems'
require 'mechanize'

a = Mechanize.new { |agent|
  agent.user_agent_alias = 'Mac Safari'
}
page = a.get('http://www.example.com/')

I need the request headers cause my page has a redirection of 2/3 times and interim of the redirection, it added few headers in it.

Upvotes: 3

Views: 4956

Answers (1)

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

you need to run a pre_connect_hooks to get the request header

 agent.pre_connect_hooks << lambda do |agent, request|
   request['X-Requested-With'] = 'XMLHttpRequest'
 end

Check this thread so-1

Read Response Header

puts page.header['location']
puts page.header['server']

loop through the header array

Upvotes: 4

Related Questions