Retrofit android @Multipart Remove default headers… here is a solution to the problem.
Retrofit android @Multipart Remove default headers
Making a @Multipart request adds these default headers (Content-Transfer-Encoding, Content-Type) to each section, is there any way to remove them?
//REQUEST BODY
--25d35373-d2c3-46a3-969f-f5a1fd5f781a
Content-Disposition: form-data; name="client_id"
Content-Transfer-Encoding: binary <-- remove this one
Content-Type: application/json; charset=UTF-8 <-- remove this one
Content-Length: 34
"40ccfee680a844780a41fbe23ea89934"
//
Note: I don’t have access to the server, so I can’t get the server to accept these headers.
Solution
You can build the multipart body yourself this way (kotlin code, but the same idea can be expressed in java):
val mpart = MultipartBody.Builder()
.addFormDataPart("param", paramValue)
.addPart(null, someRequestBody).build() // <-- (*) see explanation below
thus, service method should looks like this:
@POST("upload/endpoint")
fun upload(@Body parts: MultipartBody)
(*) – This is the addPart(headers: Headers, reqBody: RequestBody) method,
which removes the addition of ContentLength:
, when you pass null
to headers arg