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.

HttpResponse.readText() fails for non-2xx HTTP status

See original GitHub issue

Ktor Version

1.2.2

Ktor Engine Used(client or server and name)

Apache Client

JVM Version, Operating System and Relevant Context

jdk1.8.0_172, Windows

Feedback

HttpResponse.readText() fails in 1.2.2 (probably since 1.2.0) for requests that return a non-2xx HTTP reponse.

I don’t know whether this is intentional, but since it used to work in 1.1.5 and I haven’t seen any corresponding notice, this might be an unwanted regression.

See the following sample:

import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.client.request.get
import io.ktor.client.response.HttpResponse
import io.ktor.client.response.readText
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val client = HttpClient(Apache)
    val response200 = client.get<HttpResponse>("https://httpstat.us/200")
    println(response200.readText())
    // v1.2.2: Prints: "200 OK"
    // v1.1.5: Prints: "200 OK"

    val response400 = client.get<HttpResponse>("https://httpstat.us/400")
    println(response400.readText())
    // v1.2.2: Throws exception: "Exception in thread "main" io.ktor.client.features.ClientRequestException: Client request(https://httpstat.us/400) invalid: 400 Bad Request"
    // v1.1.5: Prints "400 Bad Request"
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
Should non-2xx status code responses include CORS specific ...
Yes it makes sense to have the server send CORS headers even with non-2xx responses. The reason is: without the CORS headers in...
Read more >
Response validation - Ktor
In this case, the following exceptions will be thrown for non-2xx error responses: RedirectResponseException for 3xx responses.
Read more >
Ktor Response readText() already received exception
readText() on a response that failed used to work, but doesn't anymore. readBytes() still works which makes me think that this is an...
Read more >
CDIER0916E - IBM
The REST step found a non-2xx status code in an HTTP response . ... service that the InfoSphere DataStage job invoked might have...
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