Ido Flax
07/28/2021, 7:15 PMcom.papsign.ktor.openapigen
for swagger generation
server endpoint with response (works):
route("stuff"){
jwtAuth {
route("thing").post<Unit, Response, Request, UserPrincipal>(
info("Do a thing")
) { _, request ->
thingService.createThing(
principal().username,
request.thingName
)
status(HttpStatusCode.NoContent)
respond(Response())
}
}
}
Server endpoint without response (404ing):
route("stuff"){
jwtAuth {
route("thing").post<Unit, Unit, Request, UserPrincipal>(
info("Do a thing")
) { _, request ->
thingService.createThing(
principal().username,
request.thingName
)
status(HttpStatusCode.NoContent)
respond(Unit)
}
}
}
Client:
fun createHttpClient(
authConfig: HttpAuthConfig? = null,
connectionTimeout: Int = 5_000
) = HttpClient(Apache) {
if (authConfig != null) {
Auth {
basic {
credentials {
BasicAuthCredentials(authConfig.username, authConfig.password)
}
}
}
}
engine {
socketTimeout = connectionTimeout
connectTimeout = connectionTimeout
connectionRequestTimeout = connectionTimeout * 2
}
expectSuccess = false
}
...
fun HttpClient.withJsonSupport() = config {
Json {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
accept(ContentType.Any)
}
}
...
private val jsonClient = createHttpClient().withJsonSupport()
...
<http://jsonClient.post|jsonClient.post>("$url/stuff/thing") {
headers {
append(
HttpHeaders.Authorization,
"Bearer $jwtToken"
)
}
contentType(ContentType.Application.Json)
body = Request(thingName)
}
Is it possible that ktor client doesn’t respect Unit responses?Aleksei Tirman [JB]
07/29/2021, 10:10 AMIdo Flax
07/29/2021, 10:11 AM