• Purchasing a license grants permission to use the software and to get Tech Support. A license purchase doesn't grant customization support.

Perl Upload Script - Bad Request (400)


Core license
License owner

I'm trying to create a simple perl upload script to interact with the Chevereto API.

I get the following errors:

Script Errors:
HTTP POST error code: 400
HTTP POST error message: Bad Request

Apache Error Log:
[Sun Apr 03 15:42:36.407725 2016] [:error] [pid 19919] [client] PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0
[Sun Apr 03 15:42:36.419811 2016] [:error] [pid 19919] [client] Invalid API v1 key.

I've tried different combinations of content-type without luck...

Any ideas?

use strict;
use LWP::UserAgent;
use MIME::Base64;
use JSON;

my $ua = LWP::UserAgent->new;
my $server_endpoint = "http://photos.mysite.com/api/1/upload";
my $server_api_key = "xxxx";
my $img = "/mnt/mysite/IMGP0013.JPG";

#convert the image to base64
open (my $image, $img) or die "$!";
        binmode $image;
        my $raw_string = do{ local $/ = undef; <$image>; };
        my $encoded = encode_base64( $raw_string );
close ($img);

# set custom HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');
$req->header('content-type' => 'multipart/form-data');

# add POST data to HTTP request body
my %post_hash = ( key => $server_api_key, source => $encoded, format => "json" );
my $post_data = to_json( \%post_hash );

my $resp = $ua->request($req);
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";


Founder license
Beta tester
I mean... it says your key is invalid... did you put in the right api key?


Core license
License owner
Thanks SirMoo., but I've figured out the problem.

It seem's that the API is expecting the parameters in the URI and not in the POST body (which is a little strange? - not sure?). Wonder if this tidbit can be added to the documentation.



Core license
License owner
I'm also wondering if this will have an impact trying to base64 encode a file -- it's likely not going to fit into the URI right?


Chevereto Developer
Chevereto Staff
The base64 method should be used when you are limited to _GET requests. That the whole point of it. There's no data compression or anything, is just a way to send data.

Everybody else, not limited to only _GET requests, should send the image "as is", as a _FILES object.


Core license
License owner
Hi Rodolfo - I am trying to use POST not GET?

1) When I use POST body only, I get the following errors:
Apache: - - [03/Apr/2016:21:07:06 +1000] "POST /api/1/upload HTTP/1.1" 400 531 "-" "libwww-perl/6.08"
Chevereto: [Sun Apr 03 21:07:06.455259 2016] [:error] [pid 9869] [client] Invalid API v1 key.

-- It seems to be returning Invalid API v1 key .... because no data is being interpreted...

2) When I use POST with URI and short source, it works:
Apache: - - [03/Apr/2016:21:25:43 +1000] "POST /api/1/upload/?key=xxxx&source=http%3A%2F%2Fphotos.mysite.com%2F128.jpg&format=json HTTP/1.1" 200 2630 "-" "libwww-perl/6.08"

3) When you use POST with URI and base64 source: 414
Apache: - - [03/Apr/2016:21:35:22 +1000] "POST /api/1/upload/?key=xxxx&source=<base64 snip>" 414 0 "-" "-"
Perl Client: HTTP POST error code: 414, HTTP POST error message: Request-URI Too Long

Can you help explain the _FILES object and how this should be used in POST body data?



Chevereto Developer
Chevereto Staff
You are doing it wrong in 3. The request url is just /api/v1/<action>. The other parameters are sent in the post request, not as part of the uri.


Core license
License owner
Hi Rodolfo - this is exactly what is happening in scenario one, but I keep getting "Invalid Key" because the api script is not reading the request (? I think). I also can't get (1) to work when I use a curl client.

Do you have a php/bash script that I can use to test?

Can I test my script on the demo environment?


Core license
License owner
OK - I figured it out now as you must use form url-encoded! Working now :)

Can post the perl code if anyone else interested.