HttpResponse.readText() fails for non-2xx HTTP status
See original GitHub issueKtor 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:
- Created 4 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is as designed. See https://ktor.io/clients/http-client/features/response-validation.html
Yes, sure: https://youtrack.jetbrains.com/issue/KTOR-1412