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
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].
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()
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.