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

Issue while forwarding data using LWP POST to another url

by chanakya (Friar)
on Apr 12, 2011 at 18:22 UTC ( #899020=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

I'm working on a Perl LWP script, which reads the binary data and the HTTP headers and forwards the data packet to another URL.
This is a POST request. I'm able to send the data using "cURL" request without errors but getting "403 - Forbidden" error when trying to forward the data through the script to the same url.

Below is the "cURL" request which is working fine
curl -H "User-Agent: myBrowser" -H "X-version: 2" -H "X-subject: MyTe +st001" -d@/tmp/SAMPLE.gz -X POST http://mycompany.xxx.com/2.0/post -v
Output
< HTTP/1.1 200 OK < Date: Tue, 12 Apr 2011 18:13:15 GMT < Content-Length: 0 < X-Powered-By: Servlet/2.5 JSP/2.1 * Connection #0 left intact * Closing connection #0
Below is my script (postreq.pl)

which has trouble submitting the data to the url "http://mycompany.xxx.com/2.0/post"
I use the following curl command to test my script
curl -H "User-Agent: myBrowser" -H "X-version: 2" -H "X-subject: MyTe +st001" -d@/tmp/SAMPLE.gz -X POST http://testmachine.xxx.com/cgi-bin/p +ostreq.pl -v
Perl script
#!/usr/bin/perl use strict; use HTTP::Request::Common qw{POST}; use HTTP::Response; use HTTP::Headers; use HTTP::Status qw(:constants :is status_message); use CGIWrap; use LWP::UserAgent; my $url='http://mycompany.xxx.com/2.0/post'; main(); exit(0); sub main() { my $cgi = new CGIWrap(); my $ua = LWP::UserAgent->new; my %headers = map { $_ => $cgi->http($_) } $cgi->http; my $req_headers = HTTP::Headers->new( %headers ); my $readData = sub { read(STDIN, my $buf, 65536); return $buf; }; my $req = HTTP::Request->new("POST", $url, $req_headers, $readData +) print STDOUT $cgi->header(-type=>'text/plain'); print STDOUT $req->as_string(),"\n"; my $res = $ua->request($req); if($res->is_success) { print STDOUT $res->code,' ', $res->message,"\n"; } else{ print STDOUT $res->status_line, "\n"; } }
output of the curl command while submitting data to the above script
<snip...> Content-Length: 284257 Content-Type: application/x-www-form-urlencoded < HTTP/1.1 200 OK < Date: Tue, 12 Apr 2011 18:15:16 GMT < Server: Apache < < Transfer-Encoding: chunked < Content-Type: text/plain; charset=ISO-8859-1 POST http://mycompany.xxx.com/2.0/post HTTP-USER-AGENT: myBrowser HTTP-X-SUBJECT: MyTest001 403 Forbidden * Connection #0 left intact * Closing connection #0
Please let me know the issue with the Perl snippet, is there anything i'm missing in the code

Thanks for your time

Replies are listed 'Best First'.
Re: Issue while forwarding data using LWP POST to another url
by Anonymous Monk on Apr 12, 2011 at 18:35 UTC
    What do you think 403 Forbidden means?
Re: Issue while forwarding data using LWP POST to another url
by JavaFan (Canon) on Apr 12, 2011 at 19:46 UTC
    my $req = HTTP::Request->new("POST", $url, $req_headers, $readData)
    Uhm, $url is an undeclared variable, there's no semi-colon separating this statement from the next, and $readData is a code ref, and not a string of bytes.
Re: Issue while forwarding data using LWP POST to another url
by chanakya (Friar) on Apr 13, 2011 at 02:10 UTC
    Hi JavaFan

    I missed the semicolon and declaring the $url while adding the code to PerlMonks, how do I make the $readData a string of bytes? instead of coderef?

    and "403-Forbidden" error comes when a required header is not passed to the url in $url
    Thanks for your time

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2022-12-07 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?