[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question regarding mechanize lib

Andrew Cowan

4/15/2008 5:38:00 PM

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

8 Answers

Shawn Anderson

4/15/2008 6:14:00 PM

0

[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

On Tue, Apr 15, 2008 at 10:38 AM, Andrew Cowan <icculus@gmdstudios.com>
wrote:

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

Andrew Cowan

4/15/2008 6:23:00 PM

0

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



Shawn Anderson wrote:
> 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 Cowan <icculus@gmdstudios.com>
> wrote:
>
>
>> 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
>>
>>
>>
>
>


Jesús Gabriel y Galán

4/15/2008 9:26:00 PM

0

On Tue, Apr 15, 2008 at 8:22 PM, Andrew Cowan <icculus@gmdstudios.com> 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/...

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.

Andrew Cowan

4/15/2008 11:00:00 PM

0

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 wrote:
> On Tue, Apr 15, 2008 at 8:22 PM, Andrew Cowan <icculus@gmdstudios.com> 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/...
>
> 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.
>
>
>
>


Andrew Cowan

4/16/2008 5:19:00 AM

0

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 wrote:
> On Tue, Apr 15, 2008 at 8:22 PM, Andrew Cowan <icculus@gmdstudios.com> 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/...
>
> 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.
>
>
>
>


Aman King

10/13/2008 1:18:00 PM

0

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
--
Posted via http://www.ruby-....

Gregory Brown

10/13/2008 3:31:00 PM

0

On Mon, Oct 13, 2008 at 9:18 AM, Aman King <amanking@yahoo.com> 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/mecha...

--
Technical Blaag at: http://blog.majesticseacr... | Non-tech
stuff at: http://metametta.bl...

Aaron Patterson

10/14/2008 6:40:00 AM

0

On Mon, Oct 13, 2008 at 10:18:24PM +0900, Aman King 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://exampl..., :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.

--
Aaron Patterson
http://tenderlovem...