EDIT :
I am trying to send an xml file as a post request in Android.
The server accepts text/xml. I tried creating a MultipartEntity, which has a content type of multipart/form-data.
HttpClient httpClient = new DefaultHttpClient();
/* New Post Request */
HttpPost postRequest = new HttpPost(url);
byte[] data = IOUtils.toByteArray(payload);
/* Body of the Request */
InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data), "uploadedFile");
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("uploadedFile", isb);
/* Set the Body of the Request */
postRequest.setEntity(multipartContent);
/* Set Authorization Header */
postRequest.setHeader("Authorization", authHeader);
HttpResponse response = httpClient.execute(postRequest);
InputStream content = response.getEntity().getContent();
return content;
However I get an error saying the content type cannot be consumed.
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Cannot consume content type).
How do I change the content type of the request?
Edit:
you can go this way for uploading to server
Regardless of API, content type is negotiated via a header with the 'Content-Type' key:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
You cannot control what the service expects. It's a part of their contract. You're probably sending 'text/plain' and they're expecting something in the realm of 'multipart/form-data' (think html form data).
Using your code, the following should work:
I have done something similar to access webservices. The soap request was an XML request. See the code below:
Long story short - use another constructor for your InputStreamBody that lets you specify the mime type you wish to use. If you don't, the parts in your multipart request will not have a
Content-Type
specified (see below for details). Consequently, the server does not know what type the file is, and in your case might be refusing to accept it (mine accepted them anyway, but I assume this is driven by config). If this still doesn't work, you might have a server-side issue.Note: Changing the
Content-Type
of the request itself to anything butmultipart/form-data; boundary=someBoundary
renders the request invalid; the server will not correctly parse the multipart parts.Long story long - here's my findings.
Given the following code:
The HttpClient posts the following payload (captured w/ Wireshark):
On the server, the following PHP script:
spitted out the following: