Home > database >  Call another rest api from my server in Ktor
Call another rest api from my server in Ktor

Time:09-20

I want to call another web-api from my backend on a specific request of user.

How can I achieve this in Ktor. Like in spring boot we use Rest Template, but how can I do the same in Ktor.

Reference Article for doing the same in spring boot: (Call another rest api from my server in Spring-Boot)

CodePudding user response:

You can use ktor http client

val client = HttpClient(CIO)
    val response: HttpResponse = client.get("https://ktor.io/")
    println(response.status)
    client.close()

https://github.com/ktorio/ktor-documentation/blob/2.1.1/codeSnippets/snippets/tutorial-client-get-started/src/main/kotlin/Main.kt

https://ktor.io/docs/request.html

CodePudding user response:

Use an HTTP client like Unirest to make the call to the other web api

Unirest.get("https://reqres.in/api/users/1").asString();

https://kong.github.io/unirest-java/

  • Related