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

400 Error when sending Base64 byte array


Well-Known Member
I can't figure out what I am possible doing wrong when trying to upload an image in Java by doing exactly what is mentioned in the API.

If I set ?source=<some base64 byte array> I get an error and no upload. But if ?source=<some internet image url> it works just fine.

This is my code:
 // Creates Byte Array from picture
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write((RenderedImage)image, "jpg", baos);
            URL url = new URL(URI); // http://stppl.us/api/1/upload/
            byte[] bytes = baos.toByteArray();
            // encodes picture with Base64 and inserts api key
            String base64bytes = Base64.encode(bytes);
            String data = "?key=" + KEY;
            data += "&source=" + base64bytes;
            data += "&format=json";

            // opens connection and sends data
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            DataInputStream in = new DataInputStream(conn.getInputStream());
            String decodedString;

            while (null != ((decodedString = in.readLine())))
                //temporary. Should print out a json response if working correctly

My Error:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://stppl.us/api/1/upload
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.shaneisrael.st.stppus.StppUploader.run(StppUploader.java:112)
    at java.lang.Thread.run(Unknown Source)
Any help is appreciated! Thanks


Well-Known Member
Base64 must be sent using POST and must be strict base64 format.
That is being sent as POST, by setting conn.DoOutput(true); it makes it a POST request.

I'm not sure what you mean by strict base64 format. I am doing Base64..


Well-Known Member
Just to make sure I was doing a post request, I went and downloaded the HTTP Client apache library and tried using that. As you can see in the screenshot here, this code is sending a POST with the source being Base64.

This is the response I get:

With that being said, unless you can shed any light on something I'm doing wrong or changes I need to make to server side files, I am going to assume that this is an issue in your API. I have never had such difficulty uploading files via an API until now. So all I can conclude is that there is something weird going on in the API.


Chevereto Developer
Chevereto Staff
Base64 in PHP is very sensitive to anything. Any small thing like an inline jump or thinks like that breaks the string. As is stated by the API, you can also use the _FILES method to append files.


Well-Known Member
Base64 in PHP is very sensitive to anything. Any small thing like an inline jump or thinks like that breaks the string. As is stated by the API, you can also use the _FILES method to append files.
I'm working in Java and I do not have the option to change languages thus _FILES is impossible. I've tried 3 different libraries for Base64 and even coded a base64 conversion method from scratch with the exact same results. I have checked the encoded base64 and it is all correct.

Not sure what else to do, at this point my license purchase was pointless as without getting this to work the site and chevereto have 0 merit to my project.

Here is a sample of what I am getting, and what is failing to work. Please take a look at this link and tell me what is wrong with it.


Chevereto Developer
Chevereto Staff
System uses this to check the integrity of the base64 string:

                // From _GET source should be urlencoded base64
                if(!G\timing_safe_compare(base64_encode(base64_decode($source)), $source)){
                    throw new Exception('Invalid base64 string.', 120);
That basically decodes your $source and the re-encode it and compare that with the thing that you provided. If is not the same then the system throws an exception because the base64 string is invalid for PHP. This doesn't means that the base64 is wrong, it means that for PHP that base64 string is wrong and PHP won't handle an invalid base64 string.

Since this is very annoying I've also enabled support for _FILES in the request, which you can easily achieve with cURL and with many other libraries. For example, in Java you can use this: http://stackoverflow.com/questions/17173435/send-image-file-using-java-http-post-connections