Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

LWP::USERAGENT - POST through basic authentication

by zantoro (Initiate)
on Oct 10, 2011 at 15:04 UTC ( #930656=perlquestion: print w/replies, xml ) Need Help??
zantoro has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I'm trying to push a POST request to a website with Basic Authentication, here is the code:
$req = HTTP::Request->new(POST => $self->{'baseurl'}, $args); $req->authorization_basic('', $mypassword'); $res = $self->{'agent'}->request($req); last if $res->is_success();
With this code I get a error HTTP 411 (length required). If I try adding a random length e.g.: 100000 I get: Error parsing XML - truncated response?
Does anyone know how to get this to work? Thanks, Roberto.

Replies are listed 'Best First'.
Re: LWP::USERAGENT - POST through basic authentication
by Khen1950fx (Canon) on Oct 10, 2011 at 16:55 UTC
    This might help.
    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new; $req->method("POST"); $req->uri(""); $req->authorization_basic('user', 'password'); print $ua->request($req)->as_string;
      where do I put the $args?

        Where do I put the $args

        You don't. In some of the Net modules, you can use a %args for options, but since this is LWP, there's a slightly different way to do that. For example, in the example that I gave you, I tried to keep it simple, step-by-step. Lets look at another way to do it.

        $req = HTTP::Request->new(POST => $self->{'baseurl'}, $args);

        First, you're missing a shebang line( #!/usr/bin/perl ) and strictures. You also need to declare your variables with my. Another problem is your use of $self->{'baseurl'}---What's that? You didn't declare $self nor $args. Putting it altogether:
        #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; #using for our $url my $req = HTTP::Request->new(POST => '');
        Note that the url should be absolute; also, I dropped $self->{'baseurl'} and $args because they weren't necessary and didn't do anything. Now you know where to put $args:).
Re: LWP::USERAGENT - POST through basic authentication
by Anonymous Monk on Oct 10, 2011 at 15:13 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930656]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-02-24 22:23 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (363 votes). Check out past polls.