I have an android app which uses the multipartentity from apache httpcomponents. The app uploads a file using an HTTP request to a php script. However, the file that I am uploading (.ogg) ends up in $_POST instead of $_FILES and it looks like binary, although I could be wrong.
I have the chrome rest extension and I used it to POST a multipart/form-data request with the exact same .ogg file and the file ends up in the correct spot ($_FILES).
Android code is as follows:
// Send the file to the server.
// Create HTTP objects.
// Request.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ACTION);
// Response.
HttpResponse httpResponse;
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentBody cbFile = new FileBody(mediaFile, "audio/ogg");
mpEntity.addPart("file", cbFile);
httpPost.setEntity(mpEntity);
// Execute.
httpResponse = httpClient.execute(httpPost);
// Evaluate the response.
int statusCode = httpResponse.getStatusLine().getStatusCode();
// Make sure everythings okay.
if (statusCode == 200) {
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String body = EntityUtils.toString(resEntity);
resEntity.consumeContent();
Log.d("APP_STATUS", body);
}
}
httpClient.getConnectionManager().shutdown();
And the very simple PHP script:
<?php
echo "\$_POST";
var_dump($_POST);
echo "\$_FILES";
var_dump($_FILES);
?>
Response when using the app:
Response when using the Chrome REST extension:
If anyone has any insight to what I'm failing epicly at, please let me know.
Try using the entity builder
HttpEntity mpEntity = MultipartEntityBuilder.create()
.addBinaryBody(
"file", cbFile, ContentType.create("audio/ogg"), cbFile.getName())
.build();