Last active
November 24, 2021 22:55
-
-
Save neogeogre/ebbbc70a97e231c016cc00d3dfc92eb8 to your computer and use it in GitHub Desktop.
ktor http rest client usage sample in kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.ktor.client.* | |
import io.ktor.client.engine.cio.* | |
import io.ktor.client.features.* | |
import io.ktor.client.features.auth.* | |
import io.ktor.client.features.auth.providers.* | |
import io.ktor.client.features.json.* | |
import io.ktor.client.features.logging.* | |
import io.ktor.client.request.* | |
import io.ktor.http.* | |
import io.ktor.http.ContentType.Application.Json | |
import kotlinx.coroutines.runBlocking | |
val httpClient = HttpClient(CIO) { | |
install(Logging) { logger = Logger.DEFAULT; level = LogLevel.INFO } | |
install(JsonFeature) { serializer = JacksonSerializer { ObjectMapper() } } | |
install(Auth) { basic { credentials { BasicAuthCredentials(username = user, password = pwd) } } } | |
defaultRequest { | |
header("Authorization", "Basic YWRtaW46YWRtaW4=") | |
header("dataType", "json") | |
} | |
} | |
fun post(content: ContentBean) { | |
runBlocking { | |
val response = httpClient.post<String>("$url/rest/api/content") | |
{ contentType(Json); body = content } | |
println("response is: $response") | |
} | |
} | |
// implementation "io.ktor:ktor-client-auth:$ktor_version" | |
// implementation "io.ktor:ktor-client-cio:$ktor_version" | |
// implementation "io.ktor:ktor-client-core:$ktor_version" | |
// implementation "io.ktor:ktor-client-jackson:$ktor_version" | |
// implementation "io.ktor:ktor-client-logging:$ktor_version" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import io.ktor.client.* | |
import io.ktor.client.engine.mock.* | |
import io.ktor.client.request.* | |
import io.ktor.http.* | |
import io.ktor.http.HttpStatusCode.Companion.OK | |
import kotlinx.coroutines.runBlocking | |
import mu.KotlinLogging | |
val logger = KotlinLogging.logger {} | |
private val responseHeaders = headersOf("Content-Type" to listOf(ContentType.Application.Json.toString())) | |
val mockClient: HttpClient = HttpClient(MockEngine) { | |
engine { | |
addHandler { request -> | |
when (request.url.encodedPath) { | |
"books" -> respond("""{"book1": "Zarathoustra"}""", OK, responseHeaders) | |
"comments" -> respond("""{"comment1": "It's great"}""", OK, responseHeaders) | |
else -> error("Unhandled ${request.url.encodedPath}") | |
} | |
} | |
} | |
} | |
fun main() { | |
runBlocking { | |
val response1 = mockClient.get<String> { url("/books") } | |
logger.info { response1 } | |
val response2 = mockClient.get<String> { url("/comments") } | |
logger.info { response2 } | |
} | |
} | |
//implementation "io.ktor:ktor-client-core:$ktor_version" | |
//implementation "io.ktor:ktor-client-auth:$ktor_version" | |
//implementation "io.ktor:ktor-client-cio:$ktor_version" | |
//testImplementation "io.ktor:ktor-client-mock:$ktor_version" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment