Hello, I have a sensitive server that can pass curl
requests well but there are troubles with Ktor
same requests. While I have completely same headers and data, I found 2 issues:
Ktor
adds application/json
to accept
even if it’s already used here
(i.e. accept: application/json,*/*,application/json
)
If I add accept-encoding: deflate, gzip, br, zstd
,
there is an error: io.ktor.utils.io.charsets.MalformedInputException: Input length = 1
.
But all is ok with the curl
request
Common headers:
POST / HTTP/1.1
Host: 127.0.0.1:8080
accept-encoding: deflate, gzip, br, zstd
Accept-Charset: UTF-8
Content-Length: 124
Content-Type: application/json; charset=UTF-8
Method:
suspend fun postHttpAsync(url: String, headersMap: Map<String, String>, data: Any? = null) = coroutineScope {
return@coroutineScope httpClient.post<HttpResponse>(url) {
for (element in headersMap) headers[element.key] = element.value
data?.let { body = it }
}
}
Request headers:
requestHeaders = hashMapOf(
"content-type" to "application/json; charset=UTF-8",
"Accept" to "*/*", // Bug: it becomes "*/*,application/json"
"Accept-Charset" to "UTF-8",
"accept-encoding" to "deflate, gzip, br, zstd"
)