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

Marcin Wisniowski

11/26/2020, 11:16 PM
Hello, I am using a Ktor client with the Json feature to receive some Json objects, but I also need to access the response headers. I can see them if I get the HttpResponse object, but then I don't have my deserialized Json object. How do I retrieve both the Json object and the response headers?
j

Joost Klitsie

11/27/2020, 10:01 AM
What is the usecase? Do you need to handle the headers at the same place as where you handle the response body? Because otherwise you could use an interceptor/feature to first handle headers.
j

John O'Reilly

11/27/2020, 10:08 AM
just tried here and you should be able to do something like
Copy code
suspend fun fetchPeople(): AstroResult {
    val response = client.get<HttpResponse>("$baseUrl/astros.json")
    val headers = response.headers
    val data = response.receive<AstroResult>()
    return data
}
m

Marcin Wisniowski

11/27/2020, 4:51 PM
@John O'Reilly Thank you, that's what I've been looking for.
👍 1
4 Views