Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send Image file with Retrofit(@Fields)

Here I m using @Fields data with @FormUrlEncoded But I have to use both in same API @Part("user_image") RequestBody file with @Multipart. How does it possible? Thanks in advance.

@FormUrlEncoded
@POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(@Field("user_name") String name,
                             @Field("age") String age,
                             @Field("work") String work,
                             @Field("home_town") String home_town,
                             @Field("gender") String gender,
                             @Field("interest") String interest,
                             @Field("study") String study,
                             @Field("email") String email,
                             @Field("password") String password,
                             @Field("device_id") String device_id,
                             @Field("device_type") String device_type,
                             @Part("user_image") RequestBody file,
                             @Field("signup") String signup); 
like image 565
Monika Arora Avatar asked Dec 02 '16 07:12

Monika Arora


1 Answers

Http protocol not allow 2 Content-Type in the same request. So you have to choose :

  • application/x-www-form-urlencoded
  • multipart/form-data

You use application/x-www-form-urlencoded by using annotation @FormUrlEncoded in order to send image you have to transform the whole file into text (e.g. base64).

A better approach would be use multipart/form-data by describing your request like that :

@Multipart
@POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(@Part("user_name") String name,
                         @Part("age") String age,
                         @Part("work") String work,
                         @Part("home_town") String home_town,
                         @Part("gender") String gender,
                         @Part("interest") String interest,
                         @Part("study") String study,
                         @Part("email") String email,
                         @Part("password") String password,
                         @Part("device_id") String device_id,
                         @Part("device_type") String device_type,
                         @Part("user_image") RequestBody file,
                         @Part("signup") String signup); 
like image 169
Lionel Briand Avatar answered Oct 18 '22 03:10

Lionel Briand