I am using the retrofit library for API call and I want to send the parameter to my server using the "form-data" method. I found this question on StackOverflow, but there is no solution yet. Please guide me and let me know if I can provide more details for the same. Thank you
            Asked
            
        
        
            Active
            
        
            Viewed 6,781 times
        
    3 Answers
2
            Why don't you use Multipart? This is an example of using it for a simple user info with phone number, password and a prfile pic:
In your Activity:
final RequestBody rPhoneNumber = RequestBody.create(MediaType.parse("text/plain"), "sample phone number");
final RequestBody rPassword = RequestBody.create(MediaType.parse("text/plain"), "sample phone password");
final MultipartBody.Part rProfilePicture = null;
Retrofit.Builder builder = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(baseUrl).client(Cookie.cookie.build());
Retrofit retrofit = builder.build();
final RequestHandler requestHandler = retrofit.create(RequestHandler.class);
rProfilePicture = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"),file)); //sample image file that you want to upload
Call<ServerMessage> call; //ServerMessage is a class with a String to store and convert json response
call = requestHandler.editProfile(rPhoneNumber, rPassword, rProfilePicture); //editProfile is in RequestHandler interface
call.enqueue(new Callback<ServerMessage>() {
    @Override
    public void onResponse (Call < ServerMessage > call2, Response < ServerMessage > response){
        //your code here
    }
    @Override
    public void onFailure (Call < ServerMessage > call, Throwable t) {
        //your code here
    }
});
In RequestHandler.java interface:
    @Multipart
@POST("/api/change-profile")
Call<ServerMessage> editProfile(@Part("phoneNumber") RequestBody rPhoneNumber,
                                @Part("oldPassword") RequestBody rPassword,
                                @Part MultipartBody.Part rProfilePicture);
In ServerMessage.java:
public class ServerMessage {
private String message;
public String getMessage() {
    return message;
}
}
 
    
    
        Parham
        
- 116
- 10
1
            
            
        This sample should help:
 public interface AuthService {
    @POST("register")
    @Headers("Content-Type:application/x-www-form-urlencoded")
    @FormUrlEncoded
    Call<LoginResponse> loginSocial(@Field("provider") String provider, @Field("access_token") String accessToken }
 
    
    
        bijaykumarpun
        
- 677
- 4
- 9
- 
                    1Thanks for the reply. I tried this solution, but parameters are not getting delivered to the back-end server. It is working on Postman – Dnyanesh M Aug 05 '19 at 08:11
1
            
            
        I know this might be late. I came this same challenge and this is what works for me
val requestBody: RequestBody = MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("avatar", imageFile.toString())
                        .build()
  @POST("avatar")
    fun uploadProfilePicture(
        @Header("Authorization") header: String?,
        @Body avatar:RequestBody
    ): Call<UserResponse>
 
    
    
        Darotudeen
        
- 1,914
- 4
- 21
- 36
 
    