I am trying to make an authenticated connection to a web server (well, more accurately, to a java application on a Tomcat server).
The task is to connect to a script protected by a simple http Basic Authentication, and give it a .zip file to process.
((For those who really want to know, I'm trying to make a SWORD deposit into a DSpace server))
I can do the connection using curl:
curl -i --data-binary "@./myFile.zip" -H "Content-Disposition: filenam +e=myFile.zip" -H "Content-Type:application/zip" -H "X-Packaging:http: +//purl.org/net/sword-types/METSDSpaceSIP" -H "X-No-Op:false" -H "X-Ve +rbose:true" http://myUsername:myPassword@my.host:port/path/to/script
This works.
However, I need to be able to do the web call from within a mod_perl application, so I want a perl-based transfer.... enter LWP::UserAgent. I'm starting by writing a straight script first, and then I'll tweek it to run under mod_perl.
Roughly speaking, my script equivalent is:
use LWP::UserAgent; my $host = 'my.host:port'; my $username = 'myUsername'; my $password = 'myPassword'; my $realm = 'theRealm'; my $sword_url = "http://$host/path/to/script"; my $filename = "myFile.zip"; my $file = ""; open(FILE, "$filename" ) or die('cant open input file'); binmode FILE; while(<FILE>) { $file .= $_; } close FILE; my $ua = LWP::UserAgent->new(); $ua->credentials( "$host", "$realm", "$username" => "$password" ); my $req = HTTP::Request->new( POST => $sword_url ); my $length = do {use bytes; length $file}; # Header information needed by the application $req->header( 'X-Packaging' => 'http://purl.org/net/sword-types/METSDS +paceSIP', 'X-No-Op' => 'false', 'X-Verbose' => 'true', 'Content-Disposition' => "filename=$filename" ); $req->content_type( 'application/zip' ); $req->content( $file ); $req->content_length($length); my $res = $ua->request($req); if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }
This works fine when I'm talking to a Mod_perl based application, but fails when talking to the java application (and I've tried two now) - and the failure is "401: Unauthorised"
There are two possibilities:
In reply to Replicating curl's authentication in Perl by kiz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |