Retrofit - @Body parameters cannot be used with form or multi-part encoding
This post pointed me to the right direction https://stackoverflow.com/a/21423093/1446856.
I attached everything in the body and send it as a TypedInput
.
So the interface looks something like this
@POST("/api/register")
@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")));
Adding to Julien's answer, also remove the @Multipart
annotation. Here's how I have used it:
@POST("/app/oauth/token")
Call<AuthResponse> getAuthToken(@Body RequestBody body);
And, here is how I have constructed the RequestBody
:
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("grant_type", "password")
.addFormDataPart("username", username)
.addFormDataPart("password", password)
.build();
maybe this could help some people, if you have this trouble, you should remove @FormUrlEncoded of your interface. Hope this helps.