user3045026 user3045026 - 10 months ago 80
reST (reStructuredText) Question

Retrofit for android @Multipart remove default headers

Making a @Multipart request adds these default headers (Content-Transfer-Encoding, Content-Type) for each part, is there any way to remove them?

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

NOTE: I do not have access to the server so there is no way I will be able to make the server accept these headers.

Answer Source

You can build multipart body by your self in this way (kotlin code but same idea may be expressed with java):

val mpart = MultipartBody.Builder()
            .addFormDataPart("param", paramValue)
            .addPart(null, someRequestBody).build() // <-- (*) see explanation below

//thus, service method should looks like this:
fun upload(@Body parts: MultipartBody)

(*) - this is the addPart(headers: Headers, reqBody: RequestBody) method, when you pass null to headers arg this removes all headers except Content-Length: