helix has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

I want to upload a file to a webserver, which needs authentication. Seems, as if the UserAgent does not set the credentials (because it works when I turn off server-side authentication).

BTW, the second example works and shows, that the credentials are ok.

What do I miss? Can anymonk help?

Helix
# ----- # http post, file upload # # This does NOT work use LWP::UserAgent; use HTTP::Request::Common; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->credentials("10.1.1.11:11111", "ups", "username", "password"); my $filename = "hello.txt"; my $temp_filename = "ksjbgfvksbvkns.tmp"; my $req = POST ("http://10.1.1.11:11111/put_test_blob.s2w", Content_Type => 'form-data', Content => [ fname => $temp_filename, hostname => 'lxtest1', section => 'LWP_Test', name => $filename, $temp_filename => ["$filename"], ]); my $result = $ua->request($req); print $result->as_string() , "\n"; print $result->status_line(), "\n"; # ----- # http get # # This works! use LWP::UserAgent; use URI::Escape; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->credentials("10.1.1.11:11111", "ups", "username", "password"); my $url = "http://10.1.1.111:11111/put_test_value.s2w?hostname=testhos +t&section=LWP_Test"; my $response = $ua->get($url); if ($response->is_success) { print $response->content; } else { print STDERR $response->status_line; }

Replies are listed 'Best First'.
Re: missing credentials when uploading file with POST
by zentara (Cardinal) on Apr 25, 2007 at 13:27 UTC
    Here is a upload-w-auth recipe that works. (I wrote this for https, but test it here as a http.... don't get confused by it).
    #!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common qw(POST); #test on plain http my $https_login = 'http://zentara.zentara.net/~zentara/cgi-bin/uploads +'; my $https_post = 'http://zentara.zentara.net/~zentara/cgi-bin/uploads/ +up1.cgi'; my $https_user = 'zentara'; my $https_pass = 'foobar'; my $file = 'testout.tgz'; &postHTTPS(); sub postHTTPS { my $ua = new LWP::UserAgent; $ua->protocols_allowed( [ 'http'] ); $ua->cookie_jar(HTTP::Cookies->new(file =>".cookies.txt",autos +ave => 1)); #setup request my $req = POST($https_post, Content_Type => 'multipart/form-data', Content =>[ file =>[ $file ], ], ); my $user = 'zentara'; my $pass = 'foobar'; $req->authorization_basic($user, $pass); #do post my $response = $ua->request($req); if ($response->is_error()) { printf " %s\n", $response->status_line; print "https request error!\n"; } else { my $content = $response->content(); print "$content\n"; } if ( $response->is_success ) { print $response->as_string; }else { print $response->status_line; } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      Thanks, zentara.

      That works!

      I mainly missed:

      $req->authorization_basic($user, $pass);
Re: missing credentials when uploading file with POST
by Anonymous Monk on Apr 25, 2007 at 13:11 UTC