skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    All what I did to fix my problem was remove this Annotation

    @Headers("Content-Type: application/json")  
    

    from my code and request.
    and for sending empty files you can just send a null parameter for file will take it as empty.


  2. 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]");
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search