Using an HTTP Client

I have problems with sample code:

suspend fun fetchFlight(): String {
val client = HttpClient(CIO)
return client.get’String’(FLIGHT_ENDPOINT)

It gives errors as it is, complains about this part: ‘String’ (supposed to be<> but formating hides it)
It accepts only this code, but also returns response(HttpResponse[http://kotlin-book.bignerdranch.com/2e/flight, 200 OK]), not expected data:

suspend fun fetchFlight(): String {
val client = HttpClient(CIO)
val flightResponse = client.get(FLIGHT_ENDPOINT)
return “$flightResponse”

Should I just ignore the whole chapter and look for information somewhere else? Its extremely frustrating I waste time I don’t have on outdated information.

Even though there’ll be something saying that “io.ktor:ktor-client-core:1.6.2 provides a transitive vulnerable dependency”, we have to use that version of the ktor-client-core as ktor versions 2.0 and up no longer return generics for the “get” function used in the chapter. In newer versions, it always returns an object with a type of “HttpResponse”.

However, I tried it out on 2.2.4 since I was unable to get any of the versions with the “eap” in the version name to work, and found likely how it’s supposed to be done.

What you do instead is:

val flightResponse: String = client.get(FLIGHT_ENDPOINT).body()
val loyaltyResponse: String = client.get(LOYALTY_ENDPOINT).body()

However, I think there are compatibility issues with the newer versions of ktor as when running the program, I get SLF4J errors mentioning that it failed to load the “StaticLoggerBinder”.

Might be easiest just to stick with the book and use the 1.6.2 version.

If you are using the 1.6.2 version and still getting this issue, then I’m not sure as we should be able to supply the <String> in that version.