Retrofit - @Body parameters cannot be used with form or multi-part encoding


Android Problem Overview

I m trying to make a request in which I want to include a Header , a form-urlencoded field and a json body. My Retrofit interface is as follows

Observable<RegisterResponse> register(
    @Header("Authorization") String authorization, 
    @Field("grant_type") String grantType, 
    @Body RegisterBody body

When I make this request I get back exception @Body parameters cannot be used with form or multi-part encoding.
I have also tried with the @Multipart annotation:

Observable<RegisterResponse> register(
    @Header("Authorization") String authorization, 
    @Part("grant_type") TypedString grantType, 
    @Body RegisterBody body

and I get an IllegalArgumentException and only one encoding annotation is allowed.

Android Solutions

Solution 1 - Android

maybe this could help some people, if you have this trouble, you should remove @FormUrlEncoded of your interface. Hope this helps.

Solution 2 - Android

This post pointed me to the right direction I attached everything in the body and send it as a TypedInput.
So the interface looks something like this

@Headers({ "Content-Type: application/json;charset=UTF-8"})
Observable<RegisterResponse> register(
    @Header("Authorization") String authorization,
    @Body TypedInput body

and the body looks something like this

String bodyString = jsonBody + "?grant_type=" + 
    grantType + "&scope=" + scope;
TypedInput requestBody = new TypedByteArray(
    "application/json", bodyString.getBytes(Charset.forName("UTF-8")));

Solution 3 - Android

Adding to Julien's answer, also remove the @Multipart annotation. Here's how I have used it:

Call<AuthResponse> getAuthToken(@Body RequestBody body);

And, here is how I have constructed the RequestBody:

RequestBody requestBody = new MultipartBody.Builder()
                        .addFormDataPart("grant_type", "password")
                        .addFormDataPart("username", username)
                        .addFormDataPart("password", password)

Solution 4 - Android

I solved this problem by adding the field into


like this:


it's not a good solution, but may be useful.

Solution 5 - Android

Send Authentication header with json Body to API sample code in Kotlin :

    fun callSendJsonListPost(
                      @Header("Authheader") header: String,
                      @Body list: StringBuilder
        : Observable<EntityModelUserslist>


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - AndroidJulien AthomasView Answer on Stackoverflow
Solution 2 - AndroidChrisView Answer on Stackoverflow
Solution 3 - AndroidPrinceView Answer on Stackoverflow
Solution 4 - AndroidMatt.YoungView Answer on Stackoverflow
Solution 5 - AndroidHamed JalilianiView Answer on Stackoverflow