Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found