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

Hello All,

I am new to programming Perl. I am also new to this site. Found it this morning pulling my hair out over this script that I'm writing. I am hoping someone can help me out, or point me in the right direction.

The script I'm writing is needed to send a request to another website, then receive a response (this I'll be pulling my hair out again...won't have any left after this!). From the other side, they are looking to receive something in the form of (without the comments):

# Start of Sample POST /sample/hello.aspx HTTP/1.0 Host: www.receiver.com Connection: Close Content-type: application/x-www-form-urlencoded Content-Length: 121 ID=Test&Len=121&Data=BlowfishedRealData # End of Sample

I am trying the setup LWP:UserAgent to do this. If I understand correctly, the following code would setup the header and transmit the request, then retreive the results. But, I am unsure of how to test this locally to view the data to be transmitted, with headers, and whether it's 100%. Also, I am receiving the error "Bareword "Content" not allowed while "strict subs"". Why is this a bareword? Why not the others too.

Anyway, here's the code. I'm hoping someone can help this newbie. Thanks.

#!/usr/bin/perl use vars qw( $data $len $pData $ua $req $res ); ( $data, $len, $pData, $ua, $req, $res ) = (); use strict; use LWP::UserAgent; # Create data string, and will eventually be encrypted, but later $data = "BlowfishedRealData"; # Retrieve length of data string $len = length($data); # Creating data - Part 2 $pData = "ID=Test&Len=".$len."&Data=".$data; # Sending the data $ua = LWP::UserAgent->new; $req=HTTP::Request->new(POST => 'http://www.receiver.com/hello.aspx', Host => 'www.receiver.com', Connection => 'Close', Content-type => 'application/x-www-form-urlencoded', Content => [ Data => [$pData] ] ); $res = $ua->request($req); if ($res->is_success) { print $res->as_string; } else { print "Failed: ", $res->status_line, "\n"; }

Tom
"With each day passing, what have you been doing?" Lord Buddha

Replies are listed 'Best First'.
Re: Using the HTTP::Request and Verifying Results
by borisz (Canon) on Oct 12, 2004 at 08:28 UTC
    You need to write "Content_type" => '...' instead of Content-type. Update:
    Perhaps you consider to write:
    use HTTP::Request::Common; $req = POST 'http://www.receiver.com/hello.aspx', Host => 'www.receiver.com', Connection => 'Close', Content_type => 'application/x-www-form-urlencoded', Content => [ Data => $pData ];
    or (looks more correct to me ) maybe
    use HTTP::Request::Common; $req = POST 'http://www.receiver.com/hello.aspx', Host => 'www.receiver.com', Connection => 'Close', Content_type => 'application/x-www-form-urlencoded', Content => [ ID => 'Test', Len => $len, Data => $data ];
    Boris
Re: Using the HTTP::Request and Verifying Results
by BUU (Prior) on Oct 12, 2004 at 09:14 UTC
    To elaborate on the above answer, the fat comma, =>, operator is a special quoting operator that perl has. It functions basically as a comma execept that, if the left hand side is a bareword, it is treated a string. content-type is two barewords and an operator, so bareword immediately to the left of the =>, type, is treated a string, then it evalutes the operator, -, and the second bareword, content.
      That worked. Thank you. But, how could I pass that back to myself so I could view the information it's sending. Would I have to create a Daemon, or is it easier than that?

      Tom
        print $req->as_string;
        show your request's content.
        Boris
Re: Using the HTTP::Request and Verifying Results
by knoebi (Friar) on Oct 12, 2004 at 11:06 UTC