Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

POSTing with LWP::UserAgent

by Intaglio (Novice)
on Jan 08, 2001 at 02:16 UTC ( #50394=perlquestion: print w/replies, xml ) Need Help??
Intaglio has asked for the wisdom of the Perl Monks concerning the following question:


I've checked out the perldocs for LWP::UserAgent but they don't give any specifics on how to POST with it. They give this:

$ua->request($request, $arg [, $size]) <p> Process a request, including redirects and security. This method may a +ctually send several different simple requests.

...which doesn't really help me.

Any help would be appreciated.

Replies are listed 'Best First'.
Re: POSTing with LWP::UserAgent
by chipmunk (Parson) on Jan 08, 2001 at 02:30 UTC
    The lwpcook documentation (that comes with the LWP bundle) provides very helpful examples of how to use the LWP modules. Here's the section on POSTing data:
    POST There is no simple procedural interface for posting data to a WWW serv +er. You must use the object oriented interface for this. The most common POST operation is to access a WWW form application: use LWP::UserAgent; $ua = new LWP::UserAgent; my $req = new HTTP::Request 'POST',' +limpse'; $req->content_type('application/x-www-form-urlencoded'); $req->content('match=www&errors=0'); my $res = $ua->request($req); print $res->as_string; Lazy people use the HTTP::Request::Common module to set up a suitable POST request message (it handles all the escaping issues) and has a suitable default for the content_type: use HTTP::Request::Common qw(POST); use LWP::UserAgent; $ua = new LWP::UserAgent; my $req = POST '', [ search => 'www', errors => 0 ]; print $ua->request($req)->as_string; The lwp-request program (alias POST) that is distributed with the library can also be used for posting data.
    I've used the lazy approach to good effect.
      Ah the lazy approach looks pretty good to me as well =)

      Thanks, where did you find this? The only place I located any information on LWP was at

        Into a command prompt type:
        perldoc perldoc
        and you will learn how to get documentation of Perl things installed on your system. Not to be underestimated is that you get documented the version that you actually have installed. And if you become competent (which is mainly a question of learning to use "/pattern" then "n") at finding stuff in those pages you will generally find it faster to find things in that documentation than through websites.
        You can find the lwpcook documentation on by typing lwpcook into the search box. lwpcook is an LWP CookBook; it contains "recipes" for common tasks involving LWP that you can use in own code.

        On your own system, since you have LWP installed, you should be able to read the lwpcook documentation by typing perldoc lwpcook on the command line.

        davorg wrote a good series of articles in Perl Month explaining how to get documentation using the command perldoc.

        They start in Issue #1
Re: POSTing with LWP::UserAgent
by ichimunki (Priest) on Jan 08, 2001 at 02:35 UTC
Re: POSTing with LWP::UserAgent
by Anonymous Monk on Jan 08, 2001 at 08:06 UTC
    Here's an example for you, that works OK, except for a bug that I need help with. the POST is here, and the form is below. The thing is, I can't get it to set the checkbox values and I can't find any doc anywhere to tell me how. I did this working from Orwant & Gruhl's article in TPJ 4:1. Any help would be a godsend!
    sub getLinkParse { use LWP::UserAgent; use HTTP::Request::Common; $sentence = "This is a test sentence."; $response = (new LWP::UserAgent)->request(POST ' +.cgi#submit', [ Sentence => $sentence, Constituents => "1", <<<< this is a checkbox NullLinks => "1", <<<< this is a checkbox AllLinkages => "OFF", LinkDisplay => "on", ShortLength => "6", PageFile => "/docs/submit-sentence-4.html", InputFile => "/scripts/input-to-parser", Maintainer => "sleator\" ] ); exit -1 unless $response->is_success; $_ = $response->{_content}; print $_; } This is the origianl form on the site: <FORM METHOD="POST" ACTION="/cgi-bin/link1/construct-page-4.cgi#submit +"> <textarea name="Sentence" MAXLENGTH=200 wrap=virtual rows=3 cols=70>< +/textarea><br> <INPUT TYPE="checkbox" NAME="Constituents" CHECKED>Show constituent tr +ee &nbsp; <INPUT TYPE="checkbox" NAME="NullLinks" CHECKED>Allow null links &nbsp +; <INPUT TYPE="checkbox" NAME="AllLinkages" OFF>Show all linkages &nbsp; <INPUT TYPE="HIDDEN" NAME="LinkDisplay" VALUE="on"> <INPUT TYPE="HIDDEN" NAME="ShortLength" VALUE="6"> <INPUT TYPE="HIDDEN" NAME="PageFile" VALUE="/docs/ +ml"> <INPUT TYPE="HIDDEN" NAME="InputFile" VALUE="/scripts/input-to-parser" +> <INPUT TYPE="HIDDEN" NAME="Maintainer" VALUE=""> <br> <INPUT TYPE="submit" VALUE="Submit one sentence"> <br> </FORM>
      Checkboxes without explicit VALUE fields return "ON". At least, in web browsers that follow the specifications. :)

      Try that instead of "1".

        Thanks for the suggestion re checkboxes. I tried "ON", "CHECKED", "1", "Dammit" -- none of them worked. In general, where can I find more information about this? I'm sure my next problem will have radio buttons or something else I don't know how to do! ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://50394]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-09-20 11:43 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (236 votes). Check out past polls.