Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

using LWP to POST multiple name/value pairs

by atistler (Acolyte)
on Mar 08, 2006 at 02:23 UTC ( #535086=perlquestion: print w/replies, xml ) Need Help??

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

This is more of a basic html/http question but it involves perl also. I am trying to get the contents of a web page that uses the POST method along with a few header/value pairs. When I put the following URL into my browser the resulting page is empty. I am not sure what I am doing wrong. My URL is based on the HTML form that I found in the source code. What am I missing? So this is how I am submitting my info:


This is what I see in an ethereal trace

Line-based text data: application/x-www-form-urlencoded


Here is the html form:

Display IPtoPSTNRoute

<form action="rgcgi?action=IPtoPSTNRoute" method="post"> <input type="hidden" name="type" value="DisplayRoute"> <input type="hidden" name="table" value="IPtoPSTNRoute">Phonenumber Match: <input type="text" name="phonenumbermatch">   <input type="submit" value="Display Routes"></form>
This is what my code looks like
#!/usr/bin/perl require LWP::UserAgent; use HTTP::Request::Common; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; $response = $ua->request(POST 'http://x.x.x.x/cgi-bin/rgcgi', [ type => DisplayRoute, table => IPtoPSTNRoute, phonenumbermatch => 'A1212' ]); if ($response->is_success) { print $response->content; # or whatever } else { die $response->status_line; }

Replies are listed 'Best First'.
Re: using LWP to POST multiple name/value pairs
by izut (Chaplain) on Mar 08, 2006 at 03:15 UTC
    First, make sure the rgcgi application understands GET, not just POST (in the case you're doing a GET request, calling the URL within the browser directly). Second, there's a missing parameter (action) you're not passing to rgcgi (as your html code shows us). It is missing in the Perl code you paste, too.
    http://x.x.x.x/cgi-bin/rgcgi?action=IPtoPSTNRoute&type=DisplayRoute&ta +ble=IPtoPSTNRoute&phonenumbermatch=A1212

    Igor 'izut' Sutton
    your code, your rules.

Re: using LWP to POST multiple name/value pairs
by nite_man (Deacon) on Mar 08, 2006 at 12:54 UTC
    If you need to submit some form from Perl script there is a better way to do it - use module WWW::Mechanize:
    use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->submit_form( form_number => 3, fields => { username => 'mungo', password => 'lost-and-alone', } );

    Michael Stepanov aka nite_man

    It's only my opinion and it doesn't have pretensions of absoluteness!

Re: using LWP to POST multiple name/value pairs
by zentara (Archbishop) on Mar 08, 2006 at 13:47 UTC
    here is a "recipe" for post that works. Notice there are a few differences from your code. I'm not sure which is the problem, but I suspect it is the qw(POST) needs to be imported.
    use HTTP::Request::Common qw(POST);
    #!/usr/bin/perl use warnings; use strict; require LWP::UserAgent; # There are the LWP::UserAgent # and HTTP::Request modules, # which together allow you to make POST requests . Example use HTTP::Request::Common qw(POST); my $url = ''; my $req = POST $url, [ Host => 'myHost', Server => 'Any Value', OtherField => 'Some Value', OneMoreField => 'whatever', ] ; # Just a debug to STDOUT print "HTTP-FullRequest-Header: \n"; print $req->headers->as_string() , "\n"; print "HTTP-FullRequest-Header-Content: \n"; print $req->content() ,"\n"; # Now send it use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $response = $ua->request($req); if ( $response->is_error() ) { print "Error-Code : ", $response->code() , "\n"; print "Error-Message : ", $response->message() , "\n"; } else { print $response->content() , "\n"; }

    I'm not really a human, but I play one on earth. flash japh

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535086]
Approved by spiritway
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2021-07-24 10:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found