I want to pass json object as a raw data using @Body in retrofit, but I am unable to add an image file inside the Request Object, how can I achieve it
You can convert your bitmap into a base64 string and send it to the server.
But you should send the multipart request. As I read your comment there is a list of objects that you want to send to the server. You should create a POJO that contains your list of data and then send it along with the multipart request.
Here is some code example:
class SyncUpData {
@SerializedName("Products")
var products: List<ProductDTO>? = null
@SerializedName("ProductTax")
var productTax: List<ProductTaxDTO>? = null
}
And along with your multipart request do like this:
val str = GsonBuilder().create().toJson(syncUpObj)
val jsonObject: JSONObject = JSONObject(str)
val body = jsonObject.toString(1)
val syncUp = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), body)
Your API Call:
@Multipart
@POST("")
Call<ResponseDTO> syncUpNow(@Part MultipartBody.Part[] images,
@Part("syncUp") RequestBody myObj