FAQ: How to find out what mechanize is really sending

Peter Stevens <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
Hi Andy,

Sent this as a response to the wrong email. So here it is with the right
subject line and sent to the list.

Andy Lester wrote:
> Whatever you find out, please write up something that I can put into 
> the FAQ.

In an earlier post, I talked about using Tamper Data to find out what
Firefox is really sending to the server. Here is a way to find out what
$mech is sending.

The underlying function is from HTTP::Request: $r->as_string, where $r
is an HTTP::Request object. HTML::Form has a method $form->click which
returns the HTTP::Request object (which would normally be passed to
LWP::UserAgent). So

    $f = $mech->current_form();  # or one of its cousins - gives an
                                 # HTML::Forms object
    $r = $f->click( [args] );    # returns HTTP::Request object
    $t = $r->as_string();        # text of the request to compare with
                                 # Tamper Data results.

This is not quite the same as what $mech sends, because $mech processes
the request with _modify_request, which (I believe) adds headers,
cookies and other goodies, so for this to work properly, we have
to add a step:

    $f = $mech->current_form();  # or one of its cousins - gives an
                                 # HTML::Forms object
    $r = $f->click( [args] );    # returns HTTP::Request object
    $r = $mech->_modify_request( $r );  # v1.12 internal routine.
                                 #  Compatibility in future releases?
    $t = $r->as_string();        # text of the request to compare with
                                 # Tamper Data results.

Given that this needs to use an internal function to get the right answer,
it would be nice if there were a method:

     $mech->Pretend_to_click_and_give_me_the_message()

Cheers,

Peter

P.S. Just bought your perl debugging book today. It looks really good. :-)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.