"HttpCode: 400, Errormessage: request body malformed" when calling api post

Viewed 117

I am making an android application with kotlin. I am facing the following error when calling the api. {"status":400,"errorMessage":"request body malformed."}

I think the message part is the problem.

I gave the variables messages1 and messages2 values to messages, but to no avail. (RetrofitService.sendsms value was also changed to match the type)

please help me

Here is the api guide.

  1. API Url & API Header enter image description here
  2. Request body enter image description here
  3. Response body enter image description here

Next is the code I used.

<MainActivity.kt>

    var messages1 = listOf(messages("0"+PhoneNum_to))
    var messages2 = """{"to": "${"0"+PhoneNum_to}"}"""
    timestamp = System.currentTimeMillis().toString()
    smsservice.sendsms(
        Content_Type = "application/json; charset=UTF-8",
        timestamp = timestamp,
        accesskey = "{accesskey}",
        signature = "{signKey}",
        type = "LMS",
        contentType = "COMM",
        from = "{Caller number}",
        content = "{message content}",
        messages = messages1
)?.enqueue(object : Callback<sendsms_Response> {
    override fun onResponse(call: Call<sendsms_Response>, response: Response<sendsms_Response>) {
        if(response.isSuccessful){
            // true
        } else{
            // false
    }

    override fun onFailure(call: Call<sendsms_Response>, t: Throwable) {
        // Exception
        Log.d("NaverSMSApiResult", "onFailure 에러: " + t.message.toString());
    }
})

<RetrofitService.kt>

interface RetrofitService {
    // POST 예제
    @FormUrlEncoded
    @POST("/sms/v2/services/{Service_ID}/messages")
    fun sendsms(
        @Header("Content-Type") Content_Type: String,
        @Header("x-ncp-apigw-timestamp") timestamp: String,
        @Header("x-ncp-iam-access-key") accesskey: String,
        @Header("x-ncp-apigw-signature-v2") signature: String,
        @Field("type") type: String,
        @Field("contentType") contentType: String,
        @Field("from") from: String,
        @Field("content") content: String,
        @Field("messages") messages : List<messages>
    ): Call<sendsms_Response>
}

data class messages(
    var to : String,
    var subject : String,
    var content : String,
) {
    constructor(Number : String) : this(Number, "null", "null")
}

<sendsms_Response.kt>

// DTD Create
data class sendsms_Response(
    @SerializedName("statusCode") var statusCode: String,
    @SerializedName("statusName") var statusName: String,
    @SerializedName("requestId") var requestId: String,
    @SerializedName("requestTime") var requestTime: String,
)
1 Answers
Related