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