I' trying to upload an image to server using retrofit2. I think that the way I send data is not correct but I haven't figure out yet where is my problem.
I should send the data in this format: {"file": image, "userID": userId}
This is my code in retrofit interace: @POST("avatar")
    fun uploadImage(@Body data: Data ) : Call<ResponseBody>
I have created an object Data with file and userID I don't know if this is the right way. I have seen also this example
ublic interface UploadAPIs {
@Multipart
@POST("/upload")
Call<ResponseBody> uploadImage(@Part MultipartBody.Part file, @Part("name") RequestBody requestBody);
}
But I really don't understand well, how can I sen an object with part multipart
This is how I create image file that I get from gallery in my fragment
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK)
        when(requestCode){
            GALLERY -> {
                val selectedImage= data!!.data
                val file = File(selectedImage.path)
                val data = Data(file, userID)
                val call = api!!.uploadImage(data)
                call.enqueue(object: Callback<ResponseBody>{
                    override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                        Toast.makeText(context, t.message, Toast.LENGTH_SHORT).show()
                    }
                    override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                        val response = response.body()!!.string()
                        println("Response " + response)
                        //response says user does not exist because the way I send data is not correct
                    }
                })
            }
            CAMERA -> {
             //code here
            }
        }
}
This is the class that I create for Data object
class Data (val file: File, userID: Int)
I would be grateful if anyone can help me :)
Updated code:
@Multipart
@POST("avatar")
fun uploadImage(
        @Part("userID") user_ID: RequestBody,
        @Part image: MultipartBody.Part) : Call<ResponseBody>
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK)
        when(requestCode){
            GALLERY -> {
                val selectedImage= data!!.data
                val bitmap = MediaStore.Images.Media.getBitmap(context!!.contentResolver, selectedImage)
                avatar.setImageBitmap(bitmap)
                val file = File(getRealPathFromURI(selectedImage))
                val id = RequestBody.create(MediaType.parse("text/plain"), userID.toString())
                //val reqBody = RequestBody.create(MediaType.parse(activity!!.contentResolver.getType(selectedImage)!!), file)
                val reqBody = RequestBody.create(MediaType.parse("image/*"), file)
                val body = MultipartBody.Part.createFormData("file", file.name, reqBody)
                val call = api!!.uploadImage(id, body)
                call.enqueue(object: Callback<ResponseBody>{
                    override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                        Toast.makeText(context, t.message, Toast.LENGTH_SHORT).show()
                        println("Failure")
                    }
                    override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                        if (response.isSuccessful){
                            println("Successful " + response.body()?.string())
                        }else{
                            response.errorBody()?.string()
                            println("Error " + response.headers().toString())
                        }
                    }
                })
            }
            CAMERA -> {
               //code
            }
        }
}
