0

I'm sending post request to node.js server with image and user but user body is showing empty in node server. Node server is using multer to parsing file. here is my Api.kt interface file

@Multipart
    @POST("/user/upload_avatar")
    fun uploadAvatarImage(
        @Part image:MultipartBody.Part,
        @Part("user") user: RequestBody
    ):Call<UploadImageResponse>

MainActivity.kt file

val user:String = """{"_id":"61db06b6e488c5b13211111","username":"abcda"}"""
      val multipart =MultipartBody.Part.createFormData("file",file.name,avatar)

    
        ServiceBuilder.buildService(Api::class.java).uploadAvatarImage(
            multipart,
            RequestBody.create(MediaType.parse("application/json"), user)
        ).enqueue(object :Callback<UploadImageResponse>{
            override fun onResponse(
                call: Call<UploadImageResponse>,
                response: Response<UploadImageResponse>
            ) {
               
                Toast.makeText(this@UploadImages, "Image upload successfully", Toast.LENGTH_SHORT).show()
            }

            override fun onFailure(call: Call<UploadImageResponse>, t: Throwable) {
                Toast.makeText(this@UploadImages, "Wrong With Image", Toast.LENGTH_SHORT).show()
            }

        })
vrushal
  • 1
  • 1

1 Answers1

0

First why not, making your life easier and just use ktor. With ktor you also don't have to write as much bulk code you have to write when you use a Java Libaray.

Usage example: https://medium.com/@shrikantjagtap99/uploading-multipart-form-data-using-ktor-http-client-bc3e1c6c2ce8

Kotlin is interoperable with Java, but they still have big differences in how things are done. e.g. callbacks ;)

If you still want to use retrofit:

According to: POST Multipart Form Data using Retrofit 2.0 including image

You have to use RequestBody for the image too. Maybe this solves you problem.

BierDav
  • 1,219
  • 1
  • 10
  • 27