Reputation: 5705
I have an API in which I have to send normal fields alongwith images. For calling the API I am using Retrofit. For sending the multipart request I am using the Multipart Body Builder method. Below is the code for the same.
val builder = MultipartBody.Builder()
builder.setType(MultipartBody.FORM)
var jsonArray = JSONArray()
for ((index, value) in lineItems.withIndex()) {
var jsonObject = JSONObject()
jsonObject.accumulate("Room_id", lineItems[index].roomId)
jsonObject.accumulate("jobTitle", lineItems[index].jobTitle)
jsonObject.accumulate("floorLevel", lineItems[index].floorLevel)
jsonObject.accumulate("jobTrade", lineItems[index].jobArea)
jsonObject.accumulate("jobWork", lineItems[index].jobWork)
jsonObject.accumulate("desc", lineItems[index].desc)
jsonObject.accumulate("isFixed", lineItems[index].isFixed)
jsonObject.accumulate("hourlyCost", lineItems[index].cost)
jsonObject.accumulate("hourlyTotal", lineItems[index].total)
jsonObject.accumulate("hourlyDuration", lineItems[index].duration)
jsonObject.accumulate("fixedCost", lineItems[index].fixedCost)
if (lineItems[index].lineItemId!="") {
jsonObject.accumulate("_id", lineItems[index].lineItemId)
}
jsonArray.put(jsonObject)
Log.d("json array",jsonArray.toString())
}
builder.addFormDataPart("lineHeight", jsonArray.toString())
for ((i, value) in lineItems.withIndex()) {
var imageList = ArrayList<String>()
if (lineItems[i].imageList!=null && lineItems[i].imageList!!.size>0) {
imageList = lineItems[i].imageList!!
for ((j, path) in imageList.withIndex()) {
if (!imageList[j].contains("http")) {
val file = File(path)
val requestFile =
RequestBody.create(MediaType.parse("image/"+
file.name.substring(file.name.indexOf(".")+1)), file)
val body = MultipartBody.Part.createFormData("photos" + i,
file.name, requestFile)
builder.addPart(body)
}
}
}
}
val requestBody = builder.build()
When I call the above API I get:
502 Bad Gateway
http://18.222.231.171/api/lineheight/5bb45c4485453079ebb14b15 (4637ms)
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 03 Oct 2018 10:31:23 GMT
Content-Type: text/html
Content-Length: 182
Connection: keep-alive
502 Bad Gateway
502 Bad Gateway
nginx/1.10.3 (Ubuntu)
PS:- The api works fine in iOS as well as Postman
Here is the call code:
fun addLineItems(@Header("Authorization") token: String,
@Path("jobId") jobId: String,
@Body body: okhttp3.RequestBody): Call<Response>
I confirmed with a web services guy that they get nothing in the logs when I call this API from the app, while logs are shown when same is called from Postman.
Upvotes: 1
Views: 1313
Reputation: 302
Here's how I Tried
This is how I prepared the multipart entity.
File file = new File(currentFilePath);
if (file.exists()) {
String name = URLConnection.guessContentTypeFromName(file.getName());
RequestBody requestFile = RequestBody.create(MediaType.parse(name), file);
MultipartBody.Part multipart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
}
this is the method to upload the file
@Multipart
@POST("your url")
fun uploadFile(@Header("Authorization") token: String, @Path("jobId") jobId: String,
@Part file: MultipartBody.Part): Call<Response>
Probably you should add @Multipart annotayion to your method
Upvotes: 1