question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ktor Response readText() already received exception

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
e5lcommented, Apr 5, 2021

Fixed since 1.4.3

1reaction
mobiletolycommented, May 30, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ktor Response readText() already received exception
This issue was imported from GitHub issue: https://github.com/ktorio/ktor/issues/1038. Ktor Version. 1.2.0-alpha-2. Ktor Engine Used(client or server and ...
Read more >
readText
Read the HttpResponse.content as a String. You can pass an optional charset to specify a charset in the case no one is specified...
Read more >
Parsing an error body in a ktor HTTPClient - kotlin
My query here is I am not able to access the response error body in either validateResponse or handleResponseException . Is there a...
Read more >
[Solved]-Parsing an error body in a ktor HTTPClient-kotlin
1. Define your model class for error. In my case it was something like · 2. Call the API · 1. val res...
Read more >
WebSockets - 服务器
An exception will be thrown while receiving a Frame if the client closes the connection explicitly or the TCP socket is closed. So...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found