https://kotlinlang.org logo
Title
i

Ido Flax

07/28/2021, 7:15 PM
Hello Ktor brains. We have a problem where when we have a ktor server endpoint returning a Unit response, it is not found by our ktor client (it gets a 404) where as if that same endpoint returns some non Unit response, it is found. Note we are using
com.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?
a

Aleksei Tirman [JB]

07/29/2021, 10:10 AM
I've asked for an additional information in https://youtrack.jetbrains.com/issue/KTOR-2979
i

Ido Flax

07/29/2021, 10:11 AM
@Aleksei Tirman [JB] Thanks, will be on it