Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the Referer header before loading a page with Ruby mechanize?

Is there a straightforward way to set custom headers with Mechanize 2.3?

I tried a former solution but get:

$agent = Mechanize.new
$agent.pre_connect_hooks << lambda { |p|
  p[:request]['Referer'] = 'https://wwws.mysite.com/cgi-bin/apps/Main'
} 

# ./mech.rb:30:in `<main>': undefined method `pre_connect_hooks' for nil:NilClass (NoMethodError)
like image 613
Marcos Avatar asked Dec 01 '22 23:12

Marcos


1 Answers

The docs say:

get(uri, parameters = [], referer = nil, headers = {}) { |page| ... }

so for example:

agent.get 'http://www.google.com/', [], agent.page.uri, {'foo' => 'bar'}

alternatively you might like:

agent.request_headers = {'foo' => 'bar'}
agent.get url
like image 135
pguardiario Avatar answered Dec 24 '22 16:12

pguardiario