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

I have a locally saved form /home../myForm.html, that just has to be clicked: request('$form->click') to get my statement, but I have no idea to get my local html-file 'into' the UserAgent.

get('http://...asp?USER=###&PWD=###') causes a "Failed-Login" response of the server.
Unsuccessfully I tried (beside many other things):
$req = HTTP::Request->new(GET=>'file:/../myForm.html');

For example this does not work :~(
use HTTP::Request::Common; use HTML::Form; use LWP::UserAgent; use LWP::Debug qw(+); BEGIN { *LWP::UserAgent::redirect_ok = sub {1} } use HTTP::Cookies; use File::Basename; $cookie = '/home/../cookie_jar.txt'; print unlink $cookie; $ua = LWP::UserAgent->new; $ua->cookie_jar( HTTP::Cookies->new( file => "$cookie", autosave => 1 ) ); $ua->agent('Mozilla/4.73');$c=0; # tried: file:///, file://, file:/, /myForm ... $req = HTTP::Request->new(GET => 'file:/home/../myForm.html'); # Pass request to the user agent and get a response back $res = $ua->request($req); #tried various forms of: $res = $ua->request(GET 'file:///home/../myF +orm.html'); # execution is interrupted here. $form = HTML::Form->parse( $res->content); $res = $ua->request( $form->click ); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print "Bad luck this time\n"; }
Can s.o. help me with some hints. I thank you very much for your effort, Carl (Carl.Schreiber@chello.at)

Replies are listed 'Best First'.
Re: How to get a local form to be 'clicked'?
by Joost (Canon) on Jun 05, 2002 at 16:47 UTC
    I'll assume that you have the relevant fields already filled in in your HTML file.

    What you really want then, is send a HTTP request (GET or POST) that will send the data from the form to an URL on the web. And I wouldn't use HTML::Form for this kind of thing. (except when the html form is changing a lot - but then you probably wouldn't have it on your local filesystem).

    The simplest way is probably

    Collect all the data in the local form (you can probably just hard-code this in your script) and do a

    use LWP::Simple; getprint "http://www.someurl.org/scriptname?param1=value1&param2=value +2"
    This will send a GET request with the parameters encoded in the URL. Note that you will have to escape special characters in urlencoding. You might use the URI module for that.

    See also

    perldoc LWP::Simple perldoc URI
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Of course I tried $stmt=get("http:...?field1=val1&filed2=val2");
      But this is detected as a not correct request and answered
      with a "failed-Login" html-page by the server, even in a browser:
      within the form method has to be set: method="post"

      I just have to load the local form into UserAgent and perfom an request(form->click),
      but HOW (?) can I load that damn local html-form-file?
      Isn't that just a simple thing?

      I tried:
      $req = HTTP::Request->new(GET => 'file:/home/../myForm.html'); $res = $ua->request($req);

      But $req contains: GET 'file:/home/../myForm.html'
      and then this crashes: $ua->request($req)
      Carl
        First of all, you can aslo make custom POST requests with LWP::UserAgent by using the $ua->post() call, which will also take parameters as arguments. - I would probably go for that option.

        Second, them HTML::Form documentation is not very clear on exactly what should be passed to HTML::Form->parse(), but a little browsing in the source gives me this working code:

        open HTML,"<./index.html" or die $!; my @forms = HTML::Form->parse(*HTML,'http://localhost/'); close HTML;
        Where the first argument to parse() is the typeglob that contains the filehandle of the HTML file, and the second argument is the base url, which is needed to resolve relative urls in the form.

        Hope this helps :-)

        -- Joost downtime n. The period during which a system is error-free and immune from user input.
        Got it, thank you for your support. This works:
        $res=$ua->request(POST 'http://..asp',[field1=>'val1',field2=>'val2]);

        carl
Re: How to get a local form to be 'clicked'?
by thraxil (Prior) on Jun 05, 2002 at 15:47 UTC