Keval Shukla
Keval Shukla

Reputation: 447

@Part in multipart sends the string parameters in double quote

Following API I called for Editing User Profile . I have to send user profile picture so I used multipart in API .

@Multipart
@POST(ApiURLs.EDIT_USER_PROFILE)
Call<EditProfileModel> EditUserProfile (@Part("user_id) String userId , 
@Part("user_name") String userName ,
@Part("language_id") String languageId , 
@Part("state_id") String stateId , 
@Part MultipartBody.Part 
profilePicture); 

When Service called the requested parameters would be like

"user_id" : ""23"" "user_name" : ""Keval Shukla"" "language_id": ""27"" "state_id" : "53""

how do i remove that double quote using MultiPart ?

Upvotes: 6

Views: 3288

Answers (4)

Yamini Balakrishnan
Yamini Balakrishnan

Reputation: 2411

You can send parameters other than file as RequestBody.

@Multipart
@POST(ApiURLs.EDIT_USER_PROFILE)
Call<EditProfileModel> EditUserProfile (@Part("user_id) RequestBody userId , 
@Part("user_name") RequestBody userName ,
@Part("language_id") RequestBody languageId , 
@Part("state_id") RequestBody stateId , 
@Part MultipartBody.Part profilePicture); 

To Convert String to RequestBody:

RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), userName); // Here userName is String

Upvotes: 2

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

You are doing it wrong way, when you are using MultiPart as body type you have to specify body type of each request parameter.

For example, you are sending file(image,video etc.) and string parameters. So you need to specify all the parameters and convert it to specific body type.

You need to divide parameters into two parts,

1) MultipartBody - For media file

2) RequestBody - For other string or other data type parameters

e.g.

 /*Create API Method*/
 @Multipart
 @POST("apiurl")
 Call<Object> callMethodName(@Part("mobile_no") RequestBody mobile_no, /*String param */
                             @Part("password") RequestBody password, /*String param */
                             @Part MultipartBody.Part profile_img /*file param */);

I have define Parse type as multipart/form-data, you can define as according to your requirements,

public static final String MULTIPART_TYPE = "multipart/form-data";

Now create request parameters as below, /* Adding String Params*/ RequestBody reqNumber = RequestBody.create(MediaType.parse(Constants.MULTIPART_TYPE), number.toString()); RequestBody reqPass = RequestBody.create(MediaType.parse(Constants.MULTIPART_TYPE), pass.toString());

/* Adding File*/
File file = new File(selectedImagePath);
            RequestBody requestFile = RequestBody.create(MediaType.parse(Constants.MULTIPART_TYPE), file);
            bodyFile = MultipartBody.Part.createFormData("profile_img", file.getName(), requestFile);

As last step, you need to pass request parameter to API call method as below, so it can identify parameters and send it to server.

/* Call API Method */ 
RestClient.getApiClient().callMethodName(reqNumber, reqPass, bodyFile);

Upvotes: 1

Paresh
Paresh

Reputation: 6857

It must be like -

@Multipart
@POST(ApiURLs.EDIT_USER_PROFILE)
Call<EditProfileModel> EditUserProfile (
                              @Part("user_id") RequestBody userId , 
                              @Part("user_name") RequestBody userName ,
                              @Part("language_id") RequestBody languageId , 
                              @Part("state_id") RequestBody stateId , 
                              @Part RequestBody profilePicture); 

And, to create requestBody,

File file = new File(imageURI.getPath());
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file); // File requestBody
RequestBody userName = RequestBody.create(MediaType.parse("text/plain"), userNameSTRING); // String requestBody

Upvotes: 12

Bek
Bek

Reputation: 8471

Use RequestBody instead of String.

@Part("user_id") RequestBody user_id,

To call it

String userId= "123456";
RequestBody id =
        RequestBody.create(
                okhttp3.MultipartBody.FORM, userId);

Upvotes: 0

Related Questions