Uploading image to server in Multipart along with JSON data in Android

ReachmeDroid picture ReachmeDroid · Mar 24, 2011 · Viewed 38.6k times · Source

I am trying to upload an image to a server along with some JSON data that is collected from a form.

The server has authentication.

METHOD: post

HEADERS:

Authorization   Basic d2Vic2VydmljZTpyM05hdTE3Rw==

Content-Type    multipart/form-data;boundary=xxxxxxxx

BODY:

--xxxxxxxx

Content-Disposition: form-data; name="jsonFile"
Content-Type: application/json

{"model":"Premium","deviceLongitude":4.79337638,"pseudo":"nickname","deviceLatitude":45.7671507,"year":"2005","email":"[email protected]","deviceLocale":"fr_FR","title":"my picture"}

--xxxxxxxx

Content-Disposition: form-data; name="imgName"
Content-Type: image/jpeg

//Image data array

/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAAB
AAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAB4KAD
AAQAAAABAAACgAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQH/wAARCAKAAeADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAA
AAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF
--xxxxxxxx

Answer

Bert picture Bert · May 10, 2011

The last line should be --xxxxxxxx--, and not --xxxxxxxx.