Hi Ktor community! I’m trying to reuse an HttpClie...
# ktor
m
Hi Ktor community! I’m trying to reuse an HttpClient to connect to multiple different web services. Each web service require different default requests. Is there a way to use
defaultRequest
config but only for a specific service, but all using the same HttpClient? At the moment I’m using
prepareRequest
instead, in this manner:
Copy code
suspend fun buildRequest(
    httpMethod: HttpMethod,
    endpoint: String,
    queryParams: List<Pair<String, String>> = emptyList(),
    body: String? = null
): HttpStatement {
    return httpClient.prepareRequest {
        url("${baseUrl}${endpoint}")
        queryParams.forEach { (key, values) ->
            parameter(key, values)
        }
        method = httpMethod
        basicAuth(apiKey, "")                     // These two lines and baseUrl are the same across the service
        contentType(ContentType.Application.Json) //
        body?.let { setBody(it) }
    }
}
Is this a good idea?
a
Unfortunately, using different configurations of the
DefaultRequest
plugin within a single HttpClient is not possible.