Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Part parameters can only be used with multipart encoding. (parameter #8)

Before I post this question here, I have tried to add @Multipart above interface method And searching in stackoverflow still cannot find similar with my problem.

In this case, I try to send image using TypedFile to server. My interface method look like this :

 @Headers({"Content-type: application/json"})
    @POST("/user/change")
    void postChange(@Query("name") String name, @Query("email") String  email, @Query("password") String password, @Query("phone") String phone, @Query("user_id") String userId, @Query("address[]") String[] listAddress, @Query("head[]") String[] head, @Part("photo_profile") TypedFile photoProfile, @Body TypedInput jsonObject, Callback<ReceiveDTO> callback);

EDIT

In that method we can see @Part and @Body. If i add @Multipart above the method, it will we throw an error @Body parameters cannot be used with form or multi-part encoding. (parameter #9)

I am using Retrofit 1.9

like image 392
R Besar Avatar asked Aug 29 '16 04:08

R Besar


2 Answers

For anyone with the same issue, make sure to add the @Multipart annotation above your @POST/@PUT. I had the same error and my problem was just that I was missing that @Multipart annotation.

like image 151
Miguel Rdz Avatar answered Oct 15 '22 23:10

Miguel Rdz


We use @Query only with Get Request and in fact @Query append parameters at the end of URL, See Document examples.

If you need to send user profile to server, Use MultiPart:

Multipart parts use one of Retrofit's converters or they can implement RequestBody to handle their own serialization.

For example in following piece of code we can send Profile photo with some description to server:

@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);

You can even add more additional attributes with @Part. See complete example here which step by step explained how to do this.

Edit: As JackWarthon explain here, The @Body annotation defines a single request body.

interface Foo {
  @POST("/jayson")
  FooResponse postJson(@Body FooRequest body);
}
like image 14
Amir Avatar answered Oct 15 '22 22:10

Amir