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

Hey guys, if I had a raw HTTP Request (Example from HTTP::Request::Common) :
POST http://www.perl.org/survey.cgi Content-Length: 61 Content-Type: application/x-www-form-urlencoded name=Gisle&email=gisle%40aas.no&gender=m&born=1964&trust=3%25
And wanted to use LWP::UserAgent to do it, how would it be done? I tried:
require LWP::UserAgent; my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 30, ); my $HTTPrequest = <<EOREQUEST; POST http://www.perl.org/survey.cgi Content-Length: 61 Content-Type: application/x-www-form-urlencoded name=Gisle&email=gisle%40aas.no&gender=m&born=1964&trust=3%25 EOREQUEST my $request = HTTP::Request->new($HTTPrequest); my $response = $ua->request($request); if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; }
But that didn't work. Any help would be appreciated.

Replies are listed 'Best First'.
Re: Creating A Request
by FamousLongAgo (Friar) on Nov 11, 2002 at 02:52 UTC

    Have you tried HTTP::Headers?
    use HTTP::Headers; my $h = HTTP::Headers->new(); $h->header('Content-Length' => 61 ); $h->header('Content-Type' => 'application/x-www-form-urlencod ed'); my $request = HTTP::Request->new( 'POST', 'http://www.perl.org/survey. +cgi', $h ); my $response = $ua->request( $request ); ...
    I think this is the correct usage, but it's untried. Check out the man pages for HTTP::Request and HTTP::Headers for more detail.

      This won't work, because I need to use explicitly the form of:
      POST http://www.perl.org/survey.cgi Content-Length: 61 Content-Type: application/x-www-form-urlencoded name=Gisle&email=gisle40aas.no&gender=m&born=1964&trust=3%25
      As that's what my users will be inputting. In addition, can't some of this code not concern the headers? (The input will be a HTTP request, persuant to the appropriate RFCs.)
Re: Creating A Request
by sauoq (Abbot) on Nov 11, 2002 at 03:17 UTC

    I guess what you are asking for is a method to turn a raw request into an HTTP::Request object. I don't think LWP makes such a method available. You could roll your own. As FamousLongAgo mentioned, you would need to construct your own HTTP::Headers object. Also, an HTTP::Request is a subclass of HTTP::Message and you'll have to look in the latter for the content() method which you will need to set the content of the POST request.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Creating A Request
by dws (Chancellor) on Nov 11, 2002 at 03:35 UTC
    See this node for an example of creating a POST on-the-fly. The one you're creating is missing the protocol version in the header. That might cause problems with some web servers.

Re: Creating A Request
by Enlil (Parson) on Nov 11, 2002 at 02:57 UTC
    lwpcook on CPAN offers an example of what you are asking for.

    -enlil

      I don't see it? Unless I'm missing something there's no place to put in the *whole* request, or translate the whole request into code for LWP. If I am missing something, please help!
Re: Creating A Request
by dingus (Friar) on Nov 11, 2002 at 09:37 UTC
    There is code in HTTP::Daemon (and relatives) that does the munging of headers into a LWP::UserAgent request. I'd suggest having a look there - you may want to have a look at the HTTP::Daemon::ClientConn method $c->get_request() as a start.

    Alternatively POE::Filter::HTTPD has similar code (in fact if you read the source you see its been nicked from HTTP:Daemon and hacked at), I don't know which is better, but I suspect HTTP::Daemon will be more flexible.

    Dingus


    Enter any 47-digit prime number to continue.