I am trying to make an HttpPost
with multiPart/form-data
via my android app. I have a postman test that is working with my api and the preview of that request in postman, looks like this:
POST /api/0.1/content/upload HTTP/1.1
Host: 54.221.194.167
X-AUTHORIZATION: 166e649911ff424eb14446cf398bd7d6
Cache-Control: no-cache
Postman-Token: 2412eba9-f72d-6f3b-b124-6070b5b26644
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"
{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file01"; filename="addedaslib.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
I am trying to replicate that using the multipart/form-data with my android HttpPost but it doesn't seem to be working. Is there a way to "preview" my request and see how it is actually posting to the api? what am i doing wrong? my code:
public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {
client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
HttpResponse response = null;
MultipartEntity mpe = new MultipartEntity();
try {
Log.v("API", "URL:"+url);
request.setHeader("Content-Type", "multipart/form-data");
request.addHeader("X-AUTHORIZATION",token);
request.addHeader("Cache-Control", "no-cache");
DRPContentForUpload content = new DRPContentForUpload(file);
String jsonObject = DRPJSONConverter.toJson(content);
FormBodyPart part1= new FormBodyPart("file01", new StringBody(jsonObject));
FormBodyPart part2= new FormBodyPart("file01", new FileBody(file));
mpe.addPart(part1);
mpe.addPart(part2);
//
request.setEntity(mpe);
Log.v("RAW REQUEST", "request looks like:"+mpe.toString());
response = client.execute(request);
EDIT
I was able to talk to my API team, and they said my post actually looks like this:
--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0
Content-Disposition: form-data; name="file01"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit
{"mime_type":"image/jpeg","title":"IMG_20140131_111622"}
--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0
Content-Disposition: form-data; name="file01"; filename="IMG_20140131_111622.jpg"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
That said its still not working and spitting back an error that i am missing params
here is a screen shot of the libraries included in my project:
So after searching high and low for an answer, and almost giving up, this link finally helped
here is the final working code:
public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) {
client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
HttpResponse response = null;
DRPContentForUpload content = new DRPContentForUpload(file);
String jsonObject = DRPJSONConverter.toJson(content);
String BOUNDARY= "--eriksboundry--";
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.addHeader("X-AUTHORIZATION",token);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
try {
entity.addPart("file01", new StringBody(jsonObject));
entity.addPart("file01", new FileBody(file));
request.addHeader("Accept-Encoding", "gzip, deflate");
} catch (UnsupportedEncodingException e) {
Log.v("encoding exception","E::: "+e);
e.printStackTrace();
}
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.setEntity(entity);
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}