https://kotlinlang.org logo
#ktor
Title
# ktor
j

Justin Moy

07/06/2021, 7:07 PM
hey, we're upgrading from ktor 1.4 to ktor 1.6 and are encountering an
IllegalStateException
when trying to use our ktor client to call a ktor server for our integration tests, anyone able to take a look? This is the error message when
customPost()
is called:
Failed to parse request body: request body length should be specified, chunked transfer encoding should be used or keep-alive should be disabled (connection: close)
setup:
Copy code
val httpClient = HttpClient(CIO) {
    expectSuccess = false
  }

suspend inline fun <reified TRequestData> customPost(
    url: String,
    requestCacheKey: String,
    requestContent: TRequestData,
    headers: Map<String, String>? = null,
    contentType: ContentType = ContentType.Application.Json
  ) {
        <http://httpClient.post|httpClient.post><HttpResponse>(url) {
            var contentAstString = requestCacheManager.getOrCreateRawString(requestCacheKey) { requestContent } // returns a string
            headers?.forEach { headerKey, value ->
                header(headerKey, value)
            }
            body = TextContent(contentAstString, contentType)
        }
  }
so i added logging with the client config and it started working
Copy code
this.HttpClientLogging {
      level = Level.TRACE
    }
a

Aleksei Tirman [JB]

07/07/2021, 9:44 AM
Could you please share information about a response from a server that causes client to throw
IllegalStateException
?
j

Justin Moy

07/07/2021, 3:02 PM
the ktor server in this case is returning an HTTP 202 Accepted
Copy code
call.response.status(HttpStatusCode.Accepted)
i changed the endpoint on the server to
Copy code
call.respond(HttpStatusCode.Accepted)
and it fixed it
a

Aleksei Tirman [JB]

07/07/2021, 3:51 PM
Unfortunately, I cannot reproduce
IllegalStateException
using Ktor 1.6.1
e

e5l

07/12/2021, 9:43 AM
Hey, each server handler should call
call.respond
explicitly It was a bug that using of just
call.response.status(HttpStatusCode.Accepted)
was allowed and it was reason of freezes in some cases.
8 Views