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

Ok. I give up. Hundreds of code lines working. Solved most of my issues by reading and reading and reading. Retrieved lots of data, stored it, did fancy things with it...

... and finally ... I got stuck. And finally I have to switch from passive perl monks support to active support.

So, deardear Monks, what - generally speaking - is wrong with my code (credit: not my code, copypasted from somewhere)?:

my $ua = LWP::UserAgent->new; my $server_endpoint = "https://api.someserver.com/v1/"; my $req = HTTP::Request->new(POST => $server_endpoint); $req->authorization_basic('myusername', 'mypassword'); my $post_data = "{ 'parm1': '$valueasscalar', 'parm2': 'TRUE',and: + 'so on'}"; $req->content($post_data); my $resp = $ua->request($req); if ($resp->is_success) { my $message = $resp->decoded_content; print "Received reply: $message"; } else { print "HTTP POST error code: ", $resp->code; print "HTTP POST error message: ", $resp->message; }
To be more specific: The error messages are:
HTTP POST error code: 400 HTTP POST error message: Bad Request

Is there something wrong with my code? Or is the code ok, but something wrong with the parameters?

All help is as appreciated as ever when reading on perlmonks!

Thanks, Trace on

Replies are listed 'Best First'.
Re: Bad Request 4000: HTTP::Request->new(POST => $server_endpoint)
by jmacloue (Beadle) on Mar 16, 2015 at 10:39 UTC

    Ahem, is this script supposed to send POST with JSON content? Then, first, { 'parm1': '$valueasscalar', 'parm2': 'TRUE',and: 'so on'} does not look like a valid JSON (no quotes around and), and, second, JSON may need a Content-type: application/json request header.

    Did you try you query with curl first? Something like:

    curl -v -XPOST -u 'myusername:mypassword' -H 'Content-type: applicatio +n/json' -d '{"parm1":"xxx","parm2":"yyy","and":"so on"}' https://api. +someserver.com/v1/

    The Perl code looks okay to me, so the problem must be in the request itself.

Re: Bad Request 4000: HTTP::Request->new(POST => $server_endpoint)
by tangent (Parson) on Mar 16, 2015 at 14:56 UTC
    As jmacloue points out, it looks like you are trying to send JSON data, and the $post_data is not valid JSON, and you are not setting the content-type. I find it is often easier to create the data as a normal Perl structure and then use the JSON module to encode it:
    use LWP::UserAgent; use JSON; my $ua = LWP::UserAgent->new; my $server_endpoint = "http://api.someserver.com/v1/"; my $req = HTTP::Request->new(POST => $server_endpoint); $req->authorization_basic('myusername', 'mypassword'); my $post_data = { parm1 => $valueasscalar, parm2 => \1, # this gives you a proper JSON 'true' }; my $json = encode_json($post_data); $req->content_type('application/json'); $req->content($json); my $resp = $ua->request($req); # etc
      Right! The request was rubbish. The curl-hint did half of the job: Helped debugging! The other half was switching to json! Thank you!
Re: Bad Request 4000: HTTP::Request->new(POST => $server_endpoint)
by Anonymous Monk on Mar 16, 2015 at 09:29 UTC

    Is there something wrong with my code? Or is the code ok, but something wrong with the parameters?

    $post_data could be botched ... or anything at all, there is no way to tell