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:

  1. The java application does not recognise the basic authentication defined by LWP::UserAgent, whereas it does recognise the authentication given in the URL when using the curl client
  2. The LWP::UserAgent has a problem when username contains an '@' (which is a basic requirement of the java application.)
So: does anyone know if LWP::UserAgent has issues with usernames containing '@' characters, and does anyone have a suggestion for an alternative method of connecting to the target server?


-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.

In reply to Replicating curl's authentication in Perl by kiz

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.