You want to submit form values to a CGI script from your program.
use LWP::Simple; use URI::URL; my $url = url('http://www.perl.com/cgi-bin/cpan_mod'); $url->query_form(module => 'DB_File', readme => 1); $content = get($url);
use HTTP::Request::Common qw(POST); use LWP::UserAgent; $ua = LWP::UserAgent->new(); my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod', [ module => 'DB_File', readme => 1 ]; $content = $ua->request($req)->as_string;
For simple operations, the procedural interface of the LWP::Simple module is sufficient. For fancier ones, the LWP::UserAgent module provides a virtual browser object, which you manipulate using method calls.
The format of a query string is:
In GET requests, this is encoded in the URL being requested:
Fields must still be properly escaped, so setting the
arg form parameter to
<EASY>&<FUN>" would yield:
query_form method called on a URL object correctly escapes the form values for you, or you could use the
CGI::escape_html functions on your own. In POST requests, the query string is the body of the HTTP document sent to the CGI script.
We can use the LWP::Simple module to submit data in a GET request, but there is no corresponding LWP::Simple interface for POST requests. Instead, the HTTP::Request::Common module's POST function conveniently creates a properly formatted request with everything properly escaped.
If you need to go through a proxy, construct your user agent and tell it to use a proxy this way:
$ua->proxy(['http', 'ftp'] => 'http://proxy.myorg.com:8081');