Home > Net >  Send request has two type parameters (Query, Multipart/form-data)
Send request has two type parameters (Query, Multipart/form-data)

Time:08-26

I have been tyring to execute API has two parameters as I said in title here is my API configure

//region SignUp
String SIGN_UP_USER = "User/SignUp";

@POST(SIGN_UP_USER)
@Multipart
@Headers("Content-Type: application/json")
Call<SignUpResponse> signUp(
        @Part MultipartBody.Part profilePicture,
        @Query("email") String email,
        @Query("password") String password,
        @Query("firstName") String firstName,
        @Query("lastName") String lastName);
//endregion

But when I execute the request I got this error:
java.lang.IllegalStateException: Multipart body must have at least one part.

And here is the details of request from Swagger enter image description here

Any suggestions for this issue,
Thanks in advance

CodePudding user response:

You can use @Part("email") RequestBody email, rather than @Query .

@POST(SIGN_UP_USER)
@Multipart
Call<SignUpResponse> signUp(
        @Part MultipartBody.Part profilePicture,
        @Part("email") RequestBody email,
        @Part("password") RequestBody password,
        @Part("firstName") RequestBody firstName,
        @Part("lastName") RequestBody lastName);

RequestBody email= requestBody1.create(MediaType.parse("text/plain"), "[email protected]");

CodePudding user response:

Try the below way. [I'm using Kotlin].

  1. Create Multipart Body Object

             val signUpBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
             signUpBuilder.addFormDataPart("email",email)
             signUpBuilder.addFormDataPart("password",password)
             signUpBuilder.addFormDataPart("firstName",firstName)
             signUpBuilder.addFormDataPart("lastName",lastName)
             signUpBuilder.addFormDataPart("profilePicture","filename.jpg",file)
             signUpBuilder.build()
    
  2. Pass the builder to your signup method.

Do not use multipart annotation.

@POST(SIGN_UP_USER)
fun signUp(
    @Body requestBody: MultipartBody
): Call<SignUpResponse>

Hope this will help you.

  • Related