MultipartEntity Android always return server error - android

I have two days facing a problem sending Multipart form request that include data and images, and server always give me error "Unsupported Media Type"
I have tried 3 version of apache httpclient which are (4.1, 4.3, 4.5)
Here is my code
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addTextBody("CategoryId", String.valueOf(categoryId), ContentType.TEXT_PLAIN);
multipartEntity.addBinaryBody("PrimaryImage", primaryImage, ContentType.create("image/jpeg") , "test.jpg");
I have added the following headers
request.setHeader("Accept", "application/json");
request.setHeader("Content-Disposition","form-data");
request.setHeader("Content-type","multipart/form-data");
request.setHeader("enctype", "multipart/form-data");
I am using POST Request with Digest Authentication
When I print the content of multipart form request I found the following data
--2gj-9i6ivyDT_6wE0ck0DTKUV_hwUu-6m7
Content-Disposition: form-data; name="CategoryId"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
1
--2gj-9i6ivyDT_6wE0ck0DTKUV_hwUu-6m7
Content-Disposition: form-data; name="PrimaryImage"; filename="test.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
����-�Exif
The server is keep sending me error and server admin told me that the server can't parse form-data and that the format of my request is wrong
can anyone help please
EDIT 1
When I remove the headers from my request and remove the image also the server is able to read all text parameter.
When I remove the headers from my request and keep sending then image I got SocketTimeoutException.
I don't know what is the problem

Related

Android Multipart Entity Upload

I am trying to upload StringBody using Multipart Entity in Android.
I am getting the following response from the server
--4-9m14lHqfFFgoZHvYFlrRKt2-aPnozj4d Content-Disposition: form-data; name="image" Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
hiiiii
--4-9m14lHqfFFgoZHvYFlrRKt2-aPnozj4d
I want to remove the content disposition from the response message.
How can I remove it?
Is it a server side issue or do we need to do something on the Android side?

In Android - how to build OkHttp request to upload image to flickr?

I'd appreciate any help with building the OkHttp request to upload an image to flickr.
i've searched stackoverflow, google, yahoo and everywhere else but can't seem to find a solution.
i succeed in completing the oauth authorization process, can search and download image data, etc but am stumped with trying to upload an image using OkHttp. i guess i'd manage to upload the image using plain old httpUrlConnection but i want to do it with OkHttp.
i suspect my problem lies with me not building the request/requestBody in OkHttp correctly (i don't have much knowledge of http, multiforms, etc). this is how i built my request:
final RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"photo\""),
RequestBody.create(MEDIA_TYPE_PNG, imageFile))
.build();
Request request = new Request.Builder()
.url(uri.toString())
.post(requestBody)
.build();
This is the error i get from flickr:
<rsp stat="fail">
<err code="100" msg="Invalid API Key (Key has invalid format)" />
</rsp>
(by the way i'm able to successfully upload images to imgur).
i doubt that it has anything to do with the API key as this is used successfully with the authorization for search. i think the problem may lie with the way OkHttp "builds the request".
here's what it should look like according to flickr:
If you're constructing the POST query manually instead of using some sort of service library, it should look something like this. All line endings must be \r\n.
POST /services/upload/ HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d44e178b0434
Host: api.flickr.com
Content-Length: 35261
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="api_key"
3632623532453245
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="auth_token"
436436545
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="api_sig"
43732850932746573245
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="photo"; filename="C:\test.jpg"
Content-Type: image/jpeg
{RAW JFIF DATA}
-----------------------------7d44e178b0434--
The multipart boundary should be randomly generated and should not occur anywhere inside the payload data. Also, don't forget the line ending after your RAW JFIF DATA and before the final boundary.
so my question is, how can i build the OkHttp request to match the above? i only have to include the "photo" parameter, filename and file in building the OkHttp request. all other parameters are included in the uri passed to my request.
thanks in advance
Not sure that is what you want to do here.
You would use the Flickr API's to make those requests.
https://www.flickr.com/services/developer/api/
Any site that requires login you generally have to use their API's to get an authentication token.
Hope this helps.

Android Https signing parameters

Good day!
I have a trouble with sending https request from android application with signing parameters.
I have source which allows me to send secure http request, but authentification does not work.
I need to send request to some url with port number.
By using
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
After this, I retrieve this parameters in string format like
param2=param1&param2=param2%20p - encoded format
Next I need take md5 hash and again introduce it in httpbody param without encoding
Example of request:
HTTP method - POST
Headers: Content-Length - 275
Content-Type - application/x-www-form-urlencoded
параметры в HTTP body -
PHONE=2583838&ORDER_STATE_ID=31&SOURCE=улица%20Жандосова,%20188&DESTINATION=Тепличная%20улица,%202а&CLIENTID=62&SOURCE_TIME=20130807120054&IS_PRIOR=true&NOTE=This%20is%20comment&PHONE_TO_DIAL=8(701)746-90-26&CUSTOMER=Anna%20Ahmatova&signature=ed4aaef27e2d46a46f449903f3524788
Help me please. This post request working but everytime I have trouble with authentification

How to make a chunked transfer encoding HTTP request in Android?

How to generate a chunked transfer encoding HTTP request in Android ? The code below did not do the trick for me.
HttpURLConnection cn = (HttpURLConnection)(new URL(url)).openConnection();
cn.setRequestProperty("Transfer-Encoding", "chunked");
You can't request that the response is chunked. The server isn't even required to be able to generate chunked response.
What you are doing by sending that header, is telling the server that the content of the request is in chunked encoding.

Android soap request posting -sample?

I want to send the SOAP xml request to php server from android device, as shown below are required parameter for posting request. Currently i am not getting any method or sample to do achieve.Any suggest some samples.
POST /sample/server.php HTTP/1.0
Host: x.x.x.x
User-Agent: NuSOAP/0.7.3 (1.114)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: ""
Content-Length: 551
Thanks

Categories

Resources