hi, a potential mechanize user here. i''ve looked at the API but it''s not clear what capacity there is to arbitrarily set the headers - does mechanize allow for this? mechanize is clearly great for web browsing, but i also need to construct get and post requests from scratch. i''ve looked at net:http, libcurl, openuri, but mechanize seems simpler and more functional than the others - is this an appropriate thing to do with mechanize? any tips? thanks, nick
I''m mostly just guessing here, because I haven''t gone to great lengths to explore the possibilities. But it seems like you won''t easily set arbitrary request headers, because it''s generated and used whithin the mechanize methods, and you don''t have direct access to it.>From version 0.6.10:# Fetches the URL passed in and returns a page. def get(url, referer=nil, &block) cur_page = referer || current_page || Page.new( nil, {''content-type''=>''text/html''}) # fetch the page abs_uri = to_absolute_uri(url, cur_page) request = fetch_request(abs_uri) page = fetch_page(abs_uri, request, cur_page, &block) add_to_history(page) page end As you can see, the request is generated and used immediately here - and the fetch_request and fetch_page methods are private, you can''t directly use them. If I really wanted to use mechanize AND directly manipulate the headers then I''d call those private methods using instance_eval, which allows to do that - but since those are not part of the public interface, they are more likely to change with a new version, and break your code. mortee Nick Grandy wrote:> hi, > a potential mechanize user here. i''ve looked at the API but it''s not > clear what capacity there is to arbitrarily set the headers - does > mechanize allow for this? > mechanize is clearly great for web browsing, but i also need to > construct get and post requests from scratch. i''ve looked at > net:http, libcurl, openuri, but mechanize seems simpler and more > functional than the others - is this an appropriate thing to do with > mechanize? any tips? > thanks, > nick
On Wed, Jan 16, 2008 at 01:22:26AM -0800, Nick Grandy wrote:> hi, > a potential mechanize user here. i''ve looked at the API but it''s not > clear what capacity there is to arbitrarily set the headers - does > mechanize allow for this? > mechanize is clearly great for web browsing, but i also need to > construct get and post requests from scratch. i''ve looked at > net:http, libcurl, openuri, but mechanize seems simpler and more > functional than the others - is this an appropriate thing to do with > mechanize? any tips?Its not very easy to set custom headers right now. I''m definitly open to API suggestions on this one. Basically, the best way right now is to subclass Mechanize and implement the set_headers method. For example: class MyMech < WWW::Mechanize def set_headers(uri, request, cur_page) super request.add_field(''Foo'', ''bar'') end end agent = MyMech.new page = ...... -- Aaron Patterson http://tenderlovemaking.com/
Aaron and mortee, Thanks for the tips. Looks like this is a reasonable hack to make, especially to make use of the other functionality of mechanize. Nick On Jan 17, 2008 4:05 PM, Aaron Patterson <aaron at tenderlovemaking.com> wrote:> > On Wed, Jan 16, 2008 at 01:22:26AM -0800, Nick Grandy wrote: > > hi, > > a potential mechanize user here. i''ve looked at the API but it''s not > > clear what capacity there is to arbitrarily set the headers - does > > mechanize allow for this? > > mechanize is clearly great for web browsing, but i also need to > > construct get and post requests from scratch. i''ve looked at > > net:http, libcurl, openuri, but mechanize seems simpler and more > > functional than the others - is this an appropriate thing to do with > > mechanize? any tips? > > Its not very easy to set custom headers right now. I''m definitly open > to API suggestions on this one. Basically, the best way right now is to > subclass Mechanize and implement the set_headers method. > > For example: > > class MyMech < WWW::Mechanize > def set_headers(uri, request, cur_page) > super > request.add_field(''Foo'', ''bar'') > end > end > > agent = MyMech.new > page = ...... > > -- > Aaron Patterson > http://tenderlovemaking.com/ > > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users >-- Nick Grandy mobile: (+1) 347-835-1706