Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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 ?

like image 630
Keval Shukla Avatar asked Jan 04 '23 04:01

Keval Shukla


2 Answers

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
like image 147
Paresh P. Avatar answered Jan 22 '23 06:01

Paresh P.


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
like image 43
Yamini Balakrishnan Avatar answered Jan 22 '23 05:01

Yamini Balakrishnan