Hi, a Ktor newbie here I've hard time with post fu...
# ktor
a
Hi, a Ktor newbie here I've hard time with post fun, I'm getting
400 Bad request
always even I doubled checked on the request data, the code and details in 🧵
Here is my
HttpClient
setup code:
Copy code
val httpClient = HttpClient(OkHttp) {
    install(ContentNegotiation) {
        json()
    }
    install(DefaultRequest) {
        url(<URL>)
        header("Content-Type", "application/json")
        accept(ContentType.Application.Json)
    }
    install(Logging) {
        level = LogLevel.ALL
        logger = object : Logger {
            override fun log(message: String) {
                Napier.v(tag = "AndroidHttpClient", message = message)
            }
        }
    }
Then I call the api based on this object as following:
Copy code
val response = <http://httpClient.post|httpClient.post>("CheckDeliveryLogin") {
            val internalJson = buildJsonObject {
                put("P_LANG_NO", "1")
                put("P_DLVRY_NO", userName)
                put("P_PSSWRD", password)
            }
            val json = buildJsonObject {
                put("value", internalJson)
            }
            setBody(json)
        }
The log was:
Copy code
REQUEST: <URL>
METHOD: HttpMethod(value=POST)
COMMON HEADERS
-> Accept: application/json; application/json
-> Accept-Charset: UTF-8
-> Accept-Encoding: gzip
CONTENT HEADERS
-> Content-Length: 62
-> Content-Type: application/json
BODY Content-Type: application/json
BODY START
{"value":{"P_LANG_NO":"1","P_DLVRY_NO":"1010","P_PSSWRD":"1"}}
BODY END
RESPONSE: 400 Bad Request
METHOD: HttpMethod(value=POST)
FROM: <URL>
COMMON HEADERS
-> cache-control: private
-> content-length: 1796
-> content-type: text/html
-> date: Thu, 09 Mar 2023 14:34:20 GMT
-> server: Microsoft-IIS/10.0
-> x-aspnet-version: 4.0.30319
-> x-powered-by: <http://ASP.NET|ASP.NET>
BODY Content-Type: text/html
BODY START
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>">
<html xmlns="<http://www.w3.org/1999/xhtml>">
....
BODY END
I logged the request&response info from the old project built with retrofit:
Copy code
--> POST <URL> http/1.1
Content-Type: application/json
Content-Length: 62
Host: <http://mapp.yemensoft.net|mapp.yemensoft.net>
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/4.10.0
{"Value":{"P_LANG_NO":"1","P_DLVRY_NO":"1010","P_PSSWRD":"1"}}
--> END POST (62-byte body)
<-- 200 OK <URL> (91ms)
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: <http://ASP.NET|ASP.NET>
Date: Thu, 09 Mar 2023 12:43:01 GMT
Content-Length: 120
{"Data":{"DeliveryName":"Name"},"Result":{"ErrMsg":"Successful","ErrNo":0}}
And it works fine
s
I spotted two anomalies in the request: 1. your JSON payload has
value
in the first example and
Value
in the second one 2. the Accept content-type is duplicated; this is probably because you’re adding it manually as well as via the content negotiation plugin, but I wouldn’t expect it to cause a problem
👌 1
Do you still see an error after correcting both of those?
a
Thank you! You really saved my day, it was
value
not
Value
🐕 2