Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

@FormUrlEncoded @POST("/api/register") 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:

@Multipart @FormUrlEncoded @POST("/api/register") 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.

like image 478
Chris Avatar asked Dec 05 '14 13:12

Chris


People also ask

How do you post raw whole JSON in the body of a retrofit request?

REST APIs provide us a functionality with the help of which we can add data to our database using REST API. For posting this data to our database we have to use the Post method of REST API to post our data. We can post data to our API in different formats.

What is @FormUrlEncoded in retrofit?

Annotation Type FormUrlEncodedDenotes that the request body will use form URL encoding. Fields should be declared as parameters and annotated with @Field . Requests made with this annotation will have application/x-www-form-urlencoded MIME type.


1 Answers

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

like image 80
Julien Athomas Avatar answered Sep 18 '22 23:09

Julien Athomas