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


In reply to Issue while forwarding data using LWP POST to another url by chanakya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.