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.
You can use two @Part parameters with the @Multipart encoding. One for the form encoded parts and the other for the JSON body. Retrofit will run @Parts through the converters, so make sure you're using the appropriate parameter types. In Retrofit 2 you'd want to use RequestBody (and then create the argument using FormBody.Builder) and then your JSON object type for the two.
What if parameter count is dynamic, i can send 5 to 10 parameters to same service according to condition. How can i write interface for that without @Body MyModel ?
上传接口()
@Multipart
@POST(Urls.FILE_RECEIVE)
Observable
后台提供的接口 是 参数是 String params ,File file 这样可以么
You can solve it by deleting this annotation @FormUrlEncoded
Most helpful comment
You can use two
@Partparameters with the@Multipartencoding. One for the form encoded parts and the other for the JSON body. Retrofit will run@Parts through the converters, so make sure you're using the appropriate parameter types. In Retrofit 2 you'd want to useRequestBody(and then create the argument usingFormBody.Builder) and then your JSON object type for the two.