Response Validation

This feature allows to validate HTTP response and handle transformation exceptions from engine and pipelines.

This feature is defined in the class io.ktor.client.features.HttpCallValidator and no additional artifacts are required.

Configuration

To configure response validation feature use validateResponse and handleResponseException methods:

  1. HttpResponseValidator {
  2. validateResponse { response: HttpResponse ->
  3. // ...
  4. }
  5. handleResponseException { cause: Throwable ->
  6. // ...
  7. }
  8. }

This feature could be configured multiple times; all validators and handlers are saved and called in order of install.

Expect success

The ExpectSuccess feature implemented using response validation:

  1. HttpResponseValidator {
  2. validateResponse { response ->
  3. val statusCode = response.status.value
  4. when (statusCode) {
  5. in 300..399 -> throw RedirectResponseException(response)
  6. in 400..499 -> throw ClientRequestException(response)
  7. in 500..599 -> throw ServerResponseException(response)
  8. }
  9. if (statusCode >= 600) {
  10. throw ResponseException(response)
  11. }
  12. }
  13. }

The feature is installed by default, but could be disabled in the client configuration:

  1. val client = HttpClient() {
  2. expectSuccess = false
  3. }