I'm facing 500 error when I try to upload image Using retrofit. I'm sending the image as multipart/formdata. Giving 201Post man screen shot in postman, but from our application getting 500 error. Can anyone help me to fix this. here my code
fun uploadImage(filepath: String) {
Log.e("info", ">>>>>status>>>>>" + ">>>>>>" + filepath)
val file = File(filepath)
Log.d("file...", "length..." + file.length())
val mimeType = getMimeType(file)
val metaobject = JSONObject()
metaobject.put("eye", "OD")
// for image
val requestBody: RequestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", file.name, file.asRequestBody(mimeType!!.toMediaTypeOrNull())).build()
// val requestBody: RequestBody = RequestBody.create(MultipartBody.FORM,file)
val body: MultipartBody.Part = MultipartBody.Part.createFormData("file", file.name, requestBody)
// for meta data
val metabody: RequestBody = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart(
"metadata", metaobject.toString()).build()
val header = HashMap<String, String>()
// header["Content-Type"] = "multipart/form-data; boundary=--------------------//-941843893261340198843616"
// header["Content-Length"] = file.length().toString()
header["Authorization"] =
"Bearer <<token>>"
val request = ApiService.buildService(ApiHelper::class.java)
val call = request.sendMediaFile(header,body, metabody)
call.enqueue(
object : Callback<ResponseBody?> {
override fun onResponse(
call: Call<ResponseBody?>,
response: Response<ResponseBody?>
) {
Log.e("Upload", "success" + response.body().toString())
Log.e("Upload", "success" + response.errorBody().toString())
}
override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
Log.e("Upload error:", t.message!!)
}
})
}
And API Helper class
@Multipart
@POST("files")
fun sendMediaFile(
@HeaderMap headers: Map<String, String>,
@Part image: MultipartBody.Part,
@Part("metadata") metadata: RequestBody?
): Call<ResponseBody?>
I have fixed issue. I have posted both params in single request body and changed param as @Part to @Body.
fun uploadImage(filepath: String) {
Log.e("info", ">>>>>status>>>>>" + ">>>>>>" + filepath)
val file = File(filepath)
Log.d("file...", "length..." + file.length())
val mimeType = getMimeType(file)
val metaobject = JSONObject()
metaobject.put("eye", "OD")
val requestBody: RequestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("metadata", metaobject.toString())
.addFormDataPart(
"file", file.name,
file.asRequestBody(mimeType!!.toMediaTypeOrNull())
)
.build()
val header = HashMap<String, String>()
header["Authorization"] =
"Bearer <<token>>"
val request = ApiService.buildService(ApiHelper::class.java)
val call = request.sendMediaFile(header,requestBody)
call.enqueue(
object : Callback<ResponseBody?> {
override fun onResponse(
call: Call<ResponseBody?>,
response: Response<ResponseBody?>
) {
Log.e("Upload", "success" + response.body().toString())
Log.e("Upload", "success" + response.errorBody().toString())
}
override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
Log.e("Upload error:", t.message!!)
}
})
}
and in helper class
@POST("{profileId}/files")
fun sendMediaFile(
@Path("profileId") profile_id: String,
@HeaderMap headers: Map<String, String>,
@Body metadata: RequestBody?
): Call<ResponseBody?>