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

Hello,

How do I retrieve a text file using a form submission?

I'm currently experimenting with asp scripts and trying to automate multiple requests.

I've tried using code from some of the other posts but didn't achieve my desired result.

Basically, I have an HTML form input box where the user types in the file name. Upon clicking the submit button the user is asked if they want to save the file.

Most of the perl code I've tried have given me "HTTP/1.1 100 Continue".

The following code I've used:

use strict; use warnings; use LWP::UserAgent; my $ua = new LWP::UserAgent(); my $url = "http://someurl"; my $request = new HTTP::Request( 'POST', $url ); $request->content_type( 'application/x-www-form-urlencoded' ); $request->content( 'name=value'); my $response = $ua->request( $request );
Thanks in advance.

Replies are listed 'Best First'.
Re (tilly) 1: retrieve text file using form submission
by tilly (Archbishop) on Oct 21, 2001 at 09:01 UTC
    Doing this requires a fair amount of work, which HTTP::Request::Common makes much easier. Here is a short example that works for me:
    #! /usr/local/bin/perl use strict; use LWP::UserAgent; use HTTP::Request::Common; my $ua = LWP::UserAgent->new(); my $response = $ua->request( POST 'http://localhost/cgi-bin/file.pl', Content_Type => 'form-data', Content => [ arbitrary => "with", parameters => "corresponding", here => "values", "uploaded file" => ["example.txt"], ], ); if ($response->is_success()) { print $response->content(); } else { print $response->error_as_HTML(); }
    where file.pl is a quick file upload CGI script I wrote (it just echos back the file) and "uploaded file" is the name of the cgi parameter for the uploaded file. I included some random CGI parameters just to show how to do it. For more options you can see the documentation.