Hi all, I have a simple script to POST data, but for some reason it is not posting the data:
#!/usr/bin/perl use strict; use LWP::UserAgent; use MIME::Base64; use JSON; my $ua = LWP::UserAgent->new; push @{ $ua->requests_redirectable }, 'POST'; my $server_endpoint = "http://photos.mysite.com/api/1/upload/"; my $server_api_key = "xxxx"; my $encoded = "http://photos.mysite.com/128.jpg"; my $req = HTTP::Request->new(POST => $server_endpoint); #$req->header('content-type' => 'multipart/form-data'); $req->header('content-type' => 'application/json'); my $post_json = { key => $server_api_key, source => $encoded, format => "json", }; my $post_data = encode_json($post_json); $req->content($post_data); printf "\n\n$post_data\n"; my $resp = $ua->request($req); print $resp->as_string; if ($resp->is_success) { my $message = $resp->decoded_content; print "Received reply: $message\n"; } else { print "HTTP POST error code: ", $resp->code, "\n"; print "HTTP POST error message: ", $resp->message, "\n"; }
Response in web server logs:
192.168.1.108 - - [03/Apr/2016:19:22:43 +1000] "POST /api/1/upload/ HT +TP/1.1" 301 424 "-" "libwww-perl/6.08"
You can see that the data is not being posted? I would expect to see something like: (which works when I use the Paw Client to do the POST)
192.168.1.54 - - [03/Apr/2016:19:32:44 +1000] "POST /api/1/upload/?key +=xxxx&format=json&source=http%3A%2F%2Fphotos.mysite.com%2F128.jpg HTT +P/1.1" 200 2570 "-" "Paw/2.3.3 GCDHTTPRequest"
Any ideas would be greatly appreciated. Thanks, HW

In reply to LWP not POSTING (400 Bad Request) by HollyWould

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.