Question regarding mechanize lib

A

Andrew Cowan

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
 
S

Shawn Anderson

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Andrew Cowan

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
 
J

Jesús Gabriel y Galán

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/

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.
 
A

Andrew Cowan

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


Jesús Gabriel y Galán said:
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/

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.
 
A

Andrew Cowan

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



Jesús Gabriel y Galán said:
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/

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.
 
A

Aman King

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
 
A

Aaron Patterson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top