Question regarding mechanize lib

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

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. [email protected]

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… :wink:

Thanks,
Andy

On Tue, Apr 15, 2008 at 8:22 PM, Andrew C. [email protected]
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… :wink:

I found this in the docs:

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

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.

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

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

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… :frowning:

Thanks
-Andy

On Mon, Oct 13, 2008 at 9:18 AM, Aman K. [email protected] 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

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’, :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.