'When I send images and data to formdata, I get a 400 error in android
I need to send images and data bundled with the name datas as formdata to the server in Android.
If I set all the formats, take a log value, and check it in postman as it is, it works fine. But in androidstudio, I get 400 error.
I have to send images named images as formdata and data in string and long values named datas. After changing the image from bitmap to byte array, I changed it to multipart, and changed the data to application/json format. Note that images may not be included at all, or multiple images may be included.
I've been stressed out for weeks because of this, please help me.
Below is my log value and my code.
interface --my--Interface {
@Multipart
@POST("--my address--")
fun Question(
@Header("X-ACCESS-TOKEN") X_ACCESS_TOKEN: String,
@Part images: ArrayList<MultipartBody.Part?>?,
@Part("datas") datas: RequestBody,
): Call<--my Response-->
}
fun Question(
jwt: String,
images: ArrayList<MultipartBody.Part?>?,
userIdx: Long,
CategoryIdx: Long,
title: String
) {
val --my Service-- =
getRetrofit().create(--my RetrofitInterface--::class.java)
val jsonObject =
JSONObject("{\"userIdx\":${userIdx},\"CategoryIdx\":${CategoryIdx}, \"title\":\"${title}\"}").toString()
val datas =
jsonObject.toRequestBody(contentType = "application/json".toMediaTypeOrNull())
Log.d("json/jsonObject", jsonObject)
Log.d("json/jsonBody", datas.toString())
--my View--.onLoading()
--my Service--.Question(
jwt, images, datas
).enqueue(object : Callback<--my Response--> {
override fun onResponse(
call: Call<--my Response-->,
response: Response<--my Response-->
) {
Log.d("CODING/API-RESPONSE", response.toString())
Log.d("CODING/API- response.body()", response.body().toString())
Log.d("CODING/API- response.errorbody()", response.errorBody()!!.string())
Log.d("CODING/API-RESPONSE3", response.isSuccessful.toString())
Log.d("CODING/API-images", images.toString())
if (response.isSuccessful && response.code() == 200) {
val resp = response.body()!!
Log.d("CODING/API-SUCCESS", resp.toString())
when (resp.code) {
1000 -> --my View--.onSuccess(resp.result)
else -> --my View--.onFailure(
resp.code,
resp.message
)
}
}
}
override fun onFailure(call: Call<--my Response-->, t: Throwable) {
Log.d("CODING/API-ERROR", t.message.toString())
--my View--.onFailure(400, "error.")
Log.d("CODING/API-ERROR", t.message.toString())
}
})
Log.d("API", "Hello")
Log.d("API/images", images.toString())
Log.d("API/jwt", jwt)
Log.d("API/datas, CodeQuestionReq.toString())
Log.d("API/jsonObject", jsonObject.toString())
}
Log
D/json/jsonObject: {"userIdx":13,"CategoryIdx":4,"title":"sfd"}
D/json/jsonBody: okhttp3.RequestBody$Companion$toRequestBody$2@3e80c29
D/API/images: [okhttp3.MultipartBody$Part@4220682]
D/API/datas: okhttp3.RequestBody$Companion$toRequestBody$2@3e80c29
D/API/jsonObject: {"userIdx":13,"CategoryIdx":4,"title":"sfd"}
D/CODING/APIHH: Hello
D/CODING/API-RESPONSE: Response{protocol=http/1.1, code=400, message=, url=--my url--}
D/CODING/API- response.body(): null
D/CODING/API- response.errorbody(): {"timestamp":"2022-03-28T19:50:28.712+00:00","status":400,"error":"Bad Request","path":"--my path--"}
D/CODING/API-RESPONSE3: false
D/CODING/API-images: [okhttp3.MultipartBody$Part@4220682]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
