onResponse

fun onResponse(interceptor: HttpResponse.() -> Unit)

Registers a response interceptor.

Each interceptor receives the immutable HttpResponse wrapper for the completed request. Interceptors run in registration order. They should be lightweight and must not perform blocking operations.

Example:

HttpClient {
onResponse { logger.d("HTTP ${status} for ${request.url}") }
onResponse { if (status >= 500) logger.w("Server error body(length)=${bodyLengthEstimate()}") }
}

Avoid calling suspend body-reading functions directly here if they are marked suspend—invoke them from a higher-level coroutine if needed. Prefer inspecting headers / status.

Parameters

interceptor

A lambda executed with the response as receiver after reception.