改造Android @Multipart删除默认标题 [英] Retrofit for android @Multipart remove default headers
问题描述
发出@Multipart请求会为每个部分添加这些默认标头(Content-Transfer-Encoding,Content-Type),有什么办法将其删除?
Making a @Multipart request adds these default headers (Content-Transfer-Encoding, Content-Type) for each part, 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 do not have access to the server so there is no way I will be able to make the server accept these headers.
推荐答案
您可以通过这种方式自行构建多部分主体(kotlin代码,但是用Java可以表达相同的想法):
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:
@POST("upload/endpoint")
fun upload(@Body parts: MultipartBody)
(*)-这是addPart(headers: Headers, reqBody: RequestBody)
方法,当您将null
传递给标头arg时,这会删除除Content-Length:
(*) - this is the addPart(headers: Headers, reqBody: RequestBody)
method, when you pass null
to headers arg this removes all headers except Content-Length:
这篇关于改造Android @Multipart删除默认标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!