holli has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks,

I need to drive a webpage using Win32::IE::Mechanize (Javascript issues, don't ask) and in this process I want to generate a POST request from sratch (means without having to ->get() html with some forms first).

The module sadly has no ->post() method (like LWP::UserAgent) but surely some monk has a workaround??


holli, /regexed monk/

Replies are listed 'Best First'.
Re: create a POST request from scratch using Win32::IE::Mechanize
by ikegami (Patriarch) on Mar 27, 2007 at 20:36 UTC

    I put in a lot of time to come up with a solution on a previous occasion.

    Update: Here is a module that adds a post function to Win32::IE::Mechanize.

    =pod Win32/IE/Mechanize/Post.pm Adds function post to Win32::IE::Mechanize if it doesn't already exist. usage: use Win32::IE::Mechanize::Post; -or- use Win32::IE::Mechanize::Post (); The post function accepts for arguments anything HTTP::Request::Common::POST would accept. $ie->post('http://www.example.com/survey.cgi', [ name => 'John Doe', email => 'doe@example.org', gender => 'M', born => '1976', perc => '3%', ]); # multipart/form-data $ie->post('http://www.example.com/survey.cgi', Content_Type => 'form-data', Content => [ name => 'John Doe', email => 'doe@example.org', gender => 'M', born => '1976', init => ["$ENV{HOME}/.profile"], ], ); =cut use strict; use warnings; use Win32::IE::Mechanize qw( ); package Win32::IE::Mechanize; use HTTP::Request::Common qw( ); use Win32::OLE::Variant ;# qw( Variant VT_* ); if (!*post{CODE}) { *post = sub { my $self = shift; my $agent = $self->{agent}; local $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 0; my $request = HTTP::Request::Common::POST( @_ ); my $uri = $request->uri; ($uri = $self->uri ? URI->new_abs( $uri, $self->uri->as_string ) : URI->new( $uri ) ); my $headers = $self->_extra_headers( $uri ) . $request->headers_as_string( "\015\012" ); $agent->navigate({ URL => $uri->as_string, Headers => $headers, PostData => Variant( VT_UI1, $request->content ), }); $self->_wait_while_busy; }; } 1;

    Untested. Let me know if there are any problems.

    Next step: testing it and submitting it as a patch.

Re: create a POST request from scratch using Win32::IE::Mechanize
by Corion (Patriarch) on Mar 27, 2007 at 19:46 UTC

    The only, ugly, solution I know would be to replace the HTML in the current page with a <form action="$target_url" method="POST">...</form>, but hopefully there is a better way...