Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get request headers using Mechanize?

Tags:

ruby

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.

like image 993
Sabuj Hassan Avatar asked Jun 07 '26 18:06

Sabuj Hassan


1 Answers

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

like image 166
Nikson Kanti Paul Avatar answered Jun 10 '26 16:06

Nikson Kanti Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!