# Question regarding mechanize lib

**URL:** <https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868>\
**Category:** Ruby\
**Created:** [April 15, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868 "2008-04-15T19:39:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 15, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/1 "2008-04-15T19:39:18Z")

</div>

I have been scanning the docs for mechanize and cannot find a way to  
specify or add to the request headers. Been hoping to find a quick  
example so I can set custom headers that are required for authenticating  
at specific sites (one example is Youtube’s required ‘Authorization’  
header ). I would greatly appreciate if someone could show me what I am  
missing.

Thanks!  
Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 15, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/2 "2008-04-15T20:14:36Z")

</div>

I’m not sure about generic headers, but I know one can set the basic  
authentication header by calling basic\_auth(user,pw) on their Mechanize  
object.

Hope that helps,  
/Shawn

On Tue, Apr 15, 2008 at 10:38 AM, Andrew C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 15, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/3 "2008-04-15T20:23:24Z")

</div>

Thanks Shawn, I wish it did help. I need a way to set custom request  
headers, from what I can tell Mechanize doesn’t have a public method for  
doing that, so I may likely have to revert to net/http instead. Was  
hoping I was wrong and was just missing something… 😉

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 15, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/4 "2008-04-15T23:26:33Z")

</div>

On Tue, Apr 15, 2008 at 8:22 PM, Andrew C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks Shawn, I wish it did help. I need a way to set custom request  
> headers, from what I can tell Mechanize doesn’t have a public method for  
> doing that, so I may likely have to revert to net/http instead. Was hoping I  
> was wrong and was just missing something… 😉

I found this in the docs:

[http://mechanize.rubyforge.org/mechanize/](http://mechanize.rubyforge.org/mechanize/)

go to the CHANGELOG.txt file, and there you can find this:

0.6.4  
[…]  
\* Added protected method Mechanize#set\_headers so that subclasses  
can set custom headers.  
[rubyforge.org/tracker/?func=detail&aid=7208&group\_id=1453&atid=5712](http://rubyforge.org/tracker/?func=detail&aid=7208&group_id=1453&atid=5712)

If you follow the link to the tracker you can see an example:

Mechanize can now be subclassed to add any headers a user may  
want. For example:

class A \< WWW::Mechanize  
def set\_headers(u, r, c)  
super(uri, request, cur\_page)  
request.add\_field(‘Cookie’, ‘name=Aaron’)  
request  
end  
end

I haven’t been able to test it, but it seems you can use this to  
achieve what you want. Beware that in the example the params of the  
method should actually be uri, request, cur\_page instead of u,r,c.

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 16, 2008, 7:19am UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/5 "2008-04-16T07:19:18Z")

</div>

Jesus, please ignore my last response about this, after playing with  
subclassing I found this works well. I didn’t realize the set\_headers  
method was called automatically and provided the objects it is passed by  
Mechanize, this does work to allow custom headers to be added.

Thanks for pointing this out!

-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/6 "2008-10-13T15:21:12Z")

</div>

It seems the set\_header method has been dropped in mechanize 0.8.4 (or  
maybe even before that?).

What’s the alternative now for setting custom headers?

Regards,  
Aman

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 16, 2008, 1:00am UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/7 "2008-04-16T01:00:54Z")

</div>

Hiya!

I did see that in the docs but I was unable to figure how to properly  
use it, and my searching for any examples turned up nothing…  
Specifically, I am unsure how to access some of the data that needs to  
be passed, such as the request object. The reference to cur\_page also  
confuses me as the page object is what is returned from a get or post  
call, but I need to set the headers before the post is called… All in  
all, no idea how to properly use the method… ☹

Thanks  
-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/8 "2008-10-13T17:34:03Z")

</div>

On Mon, Oct 13, 2008 at 9:18 AM, Aman K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems the set\_header method has been dropped in mechanize 0.8.4 (or  
> maybe even before that?).
> 
> What’s the alternative now for setting custom headers?

You’ll get the best help on the mechanize mailing list:  
[http://rubyforge.org/mailman/listinfo/mechanize-users](http://rubyforge.org/mailman/listinfo/mechanize-users)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 14, 2008, 8:40am UTC](https://www.ruby-forum.com/t/question-regarding-mechanize-lib/134868/9 "2008-10-14T08:40:37Z")

</div>

On Mon, Oct 13, 2008 at 10:18:24PM +0900, Aman K. wrote:

> It seems the set\_header method has been dropped in mechanize 0.8.4 (or  
> maybe even before that?).
> 
> What’s the alternative now for setting custom headers?

It depends.

You can do this:

agent.get(:uri =\> ‘[http://example.com](http://example.com)’, :headers =\> { ‘a’ =\> ‘b’ })

Or, you can set a pre connect hook to handle them:

agent.pre\_connect\_hooks \<\< lambda { |params|  
params[:request][‘a’] = ‘b’  
}

Hope that helps.
