0

I can't figure out how to send JSON object with image inside, using multipart/form-data.

POST /api/user/update 
{  id: 123,  
   user: { logo: !!here_file!!  }
}

I tried to put base64 string into the logo field, and just pass this json object, but this approach doesn't work, server needs content-type: multipart/form-data; and i can't get how to do this. I've looked through a lot of questions, but didn't find how to post JSON with file, and also this file.

Big Coach
  • 1,845
  • 2
  • 7
  • 27

2 Answers2

0

This is a generic method I'm using to send POST to the backend:

/**
 * putJSONObject
 *
 * @param url
 * @param jsonObject
 * @param timeoutMillis
 * @return
 */
protected static JSONObject putJSONObject (String url, JSONObject jsonObject, int timeoutMillis) throws IOException, JSONException {

    StringBuilder stringBuilder = new StringBuilder ();

    HttpURLConnection httpURLConnection;
    DataOutputStream printout;

    httpURLConnection = (HttpURLConnection) new URL (url).openConnection ();
    httpURLConnection.setRequestMethod ("POST");
    httpURLConnection.setReadTimeout (timeoutMillis);
    httpURLConnection.setConnectTimeout (timeoutMillis);
    httpURLConnection.setDoInput (true);
    httpURLConnection.setDoOutput (true);
    httpURLConnection.setUseCaches (false);
    httpURLConnection.connect ();

    // Send POST output.
    printout = new DataOutputStream (httpURLConnection.getOutputStream ());
    printout.writeBytes ("msg=" + URLEncoder.encode (jsonObject.toString (), "UTF-8"));
    printout.flush ();
    printout.close ();

    InputStreamReader inputStreamReader = new InputStreamReader (httpURLConnection.getInputStream ());

    int read;
    char[] buff = new char[4096];
    while ((read = inputStreamReader.read (buff)) != -1) {
        stringBuilder.append (buff, 0, read);
    }
    httpURLConnection.disconnect ();

    return new JSONObject (stringBuilder.toString ());

}

The JSON sent is sent as 'msg'

and for encoding the picture to an string this is my code:

/**
 * toBase64
 *
 * @param bitmap
 * @return String
 */
public static String toBase64 (Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

/**
 * fromBase64
 *
 * @param encodedImage
 * @return Bitmap
 */
public static Bitmap fromBase64 (String encodedImage) {
    byte[] decodedByte = Base64.decode(encodedImage, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

Hope it helps you.

Walter Palladino
  • 479
  • 1
  • 3
  • 8
0

At first you should change the approach. Before you send JSON object wherever you want you have to load you image (file) to the upload server. Upload server it is a Server where you'll store your image and can access it by reference. It looks like this: you upload an image to the server using multipart / form-data and get a link to the image. Then you put this link into your JSON object like

{  
   id: 123,  
   user: { logo: https://myuploadserver.com/img123.jpg }
}

Then you can do with JSON object all you want

A few links to the stackoverflow with a description of how to upload data to the upload server using multipart/form-data :

1.simple HttpURLConnection POST file multipart/form-data
2.upload multipart form data to server

Vlad Pylyp
  • 314
  • 3
  • 9