• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.
  • Chevereto Support CLST

    Support response

    Support checklist

400 Error when sending Base64 byte array

shanei

Chevereto 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:
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();
            conn.setDoOutput(true);
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.writeBytes(data);
            out.flush();
            out.close();
           
            DataInputStream in = new DataInputStream(conn.getInputStream());
            String decodedString;

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

            in.close();
            //wr.close();

My Error:
Code:
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
 
Base64 must be sent using POST and must be strict base64 format.
 
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..
 
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.

V6t5kRq.png


This is the response I get:
KlUglIq.png


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.
 
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.
 
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.
Code:
http://stppl.us/api/1/upload/?key=f5d630e655b3806f24d81d0fe4715590&source=%2F9j%2F2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAx%0ANDQ0Hyc5PTgyPC4zNDL%2F2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy%0AMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL%2FwAAUCAAJABUEASIAAhEBAxEBBCIA%2F8QAHwAAAQUB%0AAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL%2F8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEG%0AE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVW%0AV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLD%0AxMXGx8jJytLT1NXW19jZ2uHi4%2BTl5ufo6erx8vP09fb3%2BPn6%2F8QAHwEAAwEBAQEBAQEBAQAAAAAA%0AAAECAwQFBgcICQoL%2F8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKR%0AobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hp%0AanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU%0A1dbX2Nna4uPk5ebn6Onq8vP09fb3%2BPn6%2F9oADgQBAAIRAxEEAAA%2FAPCKKB2%2BlIetdJke%2FwBFFFFF%0AFFABRRRX%2F9k%3D&format=txt
 
System uses this to check the integrity of the base64 string:

PHP:
                // 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
 
Back
Top