Retrofit for android @Multipart remove default headers

▼魔方 西西 提交于 2019-12-07 07:37:37

问题


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.


回答1:


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)

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



来源:https://stackoverflow.com/questions/39846958/retrofit-for-android-multipart-remove-default-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!