Search code examples
retrofitcontent-typemultipart

Retrofit change multipart entries Content-Type


I'm using Retrofit 2, and I have following method in my service:

@Multipart
@POST("avatar.ashx")
Call<ResponseBody> getAvatar(@Header("Authorization") String auth,
                             @Part(value = "l", encoding = "8bit") String login,
                             @Part(value = "p", encoding = "8bit") String password);

The server is expecting request with parts with

Content-Type: text/plain; charset=UTF-8

But what retrofit produces in request is

Content-Type: application/json; charset=UTF-8

Is there any way to change the Content-Type?


Solution

  • Maybe someone needs that, so I will post workaround I've found: Method in my service:

        @Multipart
        @POST("avatar.ashx")
        Call<ResponseBody> getAvatar(@Header("Authorization") String auth,
                                 @PartMap() Map<String, RequestBody> requestData);
    

    And usage of the method:

        LinkedHashMap<String, RequestBody> requestData = new LinkedHashMap<>();
        RequestBody rb;
        rb = RequestBody.create(MediaType.parse("text/plain"), getSettings().getUserName());
        requestData.put("l", rb);
        rb = RequestBody.create(MediaType.parse("text/plain"), getSettings().getPassword());
        requestData.put("p", rb);
        mCall = getAvatarApiService().getAvatar(auth, requestData);