Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Created July 20, 2021 23:37
Show Gist options
  • Save markchristopherng/3837372fe1d5a96ea8e8fd7ad5146fac to your computer and use it in GitHub Desktop.
Save markchristopherng/3837372fe1d5a96ea8e8fd7ad5146fac to your computer and use it in GitHub Desktop.
fun Application.module() {
val jsonParser = Json {
ignoreUnknownKeys = true
isLenient = true
}
install(ContentNegotiation) {
json(jsonParser)
}
val client = HttpClient(Apache) {
install(JsonFeature) {
serializer = KotlinxSerializer(jsonParser)
}
}
routing {
post("/greeting") {
val greetingRequest = call.receive<GreetingRequest>()
val ipAddress = client.get<IpAddress>("https://api.ipify.org?format=json")
call.respond(GreetingResponse("Hello ${greetingRequest.firstName} ${greetingRequest.lastName}", ipAddress.ip))
}
}
}
@Serializable
class IpAddress(var ip: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment