Ktor: Ktor Response readText() already received exception

Created on 22 Mar 2019  路  10Comments  路  Source: ktorio/ktor

Ktor Version

1.2.0-alpha-2

Ktor Engine Used(client or server and name)

Client OkHttp

JVM Version, Operating System and Relevant Context

JVM 1.8

Feedback

readText() on a response that failed used to work, but doesn't anymore.
readBytes() still works which makes me think that this is an undesirable behaviour.

If it is the desired behaviour, I'd love the rationale behind it as well as the best way to transform the response body into a String.

val client = HttpClient(MockEngine { MockHttpResponse(it.call, HttpStatusCode.BadRequest) })

try {
    client.get<String>("www.google.com")
} catch (exception: ResponseException) {
    // io.ktor.client.call.DoubleReceiveException: Response already received: io.ktor.client.call.HttpClientCall@27ce24aa
    exception.response.readText()
    // This is fine
    String(exception.response.readBytes())
}

Stacktrace

io.ktor.client.call.DoubleReceiveException: Response already received: io.ktor.client.call.HttpClientCall@27ce24aa

    at io.ktor.client.call.HttpClientCall.receive(HttpClientCall.kt:57)
    at io.ktor.client.response.HttpResponseKt.readText(HttpResponse.kt:80)
    at io.ktor.client.response.HttpResponseKt.readText$default(HttpResponse.kt:74)
    at suite.TestSuiteMultiCluster$stuff$1.invokeSuspend(TestSuiteMultiCluster.kt:84)
bug triaged

All 10 comments

Fixed in 1.2.0

I have the same issue in 1.2.1.
context.response.readText() produce exeption DoubleReceiveException

Also having this problem on 1.2.2, readBytes works fine but readText or receive do not.

@e5l Can we reopen this issue? I have the same problem with 1.2.3

1.2.5 no changes

still present in 1.3.2 . Reproduced on Android client as well

Same issue for me. When I call LinkedIn API and it returns 409 I want to parse the response to get the LinkedIn error message:

HttpResponseValidator {
  validateResponse { response ->
    if (response.status.value !in 200..299) {
      val error = jacksonObjectMapper().readValue<LinkedinErrorResponseDTO>(response.readText())
      throw LinkedinException(error.message)
    }
  }
}

readText() fails with io.ktor.client.call.DoubleReceiveException: Response already received

How should I parse the error response then?

@minogin I just encountered the same issue, luckily there is a work-around. While response.readText() throws an exception, you should still be able to use response.readBytes() call and convert it to text or deserialize to JSON response.

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Fixed since 1.4.3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KennethanCeyer picture KennethanCeyer  路  4Comments

seanf picture seanf  路  3Comments

PatrickLemke picture PatrickLemke  路  3Comments

dedward3 picture dedward3  路  4Comments

gabin8 picture gabin8  路  3Comments