0

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

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

0 Answers0