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

2

29

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

Bashful answered 29/8, 2016 at 4:57 Comment(1)
I would suggest follow the example here - futurestud.io/blog/retrofit-2-how-to-upload-files-to-server using multipart and then edit question with the problem you are facingSartain
H
82

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.

Hexane answered 7/11, 2018 at 0:44 Comment(0)
W
15

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);
}
Wanderjahr answered 29/8, 2016 at 5:9 Comment(1)
I still got an error @Body parameters cannot be used with form or multi-part encoding. (parameter #9). It because there is @Body in last parameter.Bashful

© 2022 - 2024 — McMap. All rights reserved.