How to set the Referer header before loading the page using the Ruby engine?

Is there an easy way to set custom headers using Mechanize 2.3?

I tried the previous solution , but I 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)
+3
source share
3 answers

On this issue, I noticed that people seem to use:

page = agent.get("http://www.you.com/index_login/", :referer => "http://www.you.com/")

On the sidelines, now that I have tested this answer, it seems that this is not the problem of my real problem: every visit to the site that I clear requires a second look at the pages of the login sequence, even a few seconds after the first login, despite the fact that that I always download and save the full cookie doll in yaml format. But this, of course, will lead to another question.

+2

:

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

, :

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

:

agent.request_headers = {'foo' => 'bar'}
agent.get url
+9

You misunderstood the code you copied. In the example, there was a new line, but it disappeared in the formatting, because it was not marked as code. $agentcontains nilbecause you are trying to use it before initializing it. You must initialize the object and then use it. Just try the following:

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

All Articles