Response

An HTTP response. Instances of this class are not immutable: the response body is a one-shot value that may be consumed only once and then closed. All other properties are immutable.

This class implements Closeable. Closing it simply closes its response body. See ResponseBody for an explanation and examples.

Types

Link copied to clipboard
open class Builder

Properties

Link copied to clipboard
@get:JvmName(name = "body")
val body: ResponseBody

Returns a non-null value if this response was passed to Callback.onResponse or returned from Call.execute. Response bodies must be closed and may be consumed only once.

Link copied to clipboard
@get:JvmName(name = "cacheControl")
val cacheControl: CacheControl

Returns the cache control directives for this response. This is never null, even if this response contains no Cache-Control header.

Link copied to clipboard
@get:JvmName(name = "cacheResponse")
val cacheResponse: Response?

Returns the raw response received from the cache. Will be null if this response didn't use the cache. For conditional get requests the cache response and network response may both be non-null. The body of the returned response should not be read.

Link copied to clipboard
@get:JvmName(name = "code")
val code: Int

Returns the HTTP status code.

Link copied to clipboard
@get:JvmName(name = "handshake")
val handshake: Handshake?

Returns the TLS handshake of the connection that carried this response, or null if the response was received without TLS.

Link copied to clipboard
@get:JvmName(name = "headers")
val headers: Headers

Returns the HTTP headers.

Link copied to clipboard

Returns true if this response redirects to another resource.

Link copied to clipboard

Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.

Link copied to clipboard
@get:JvmName(name = "message")
val message: String

Returns the HTTP status message.

Link copied to clipboard
@get:JvmName(name = "networkResponse")
val networkResponse: Response?

Returns the raw response received from the network. Will be null if this response didn't use the network, such as when the response is fully cached. The body of the returned response should not be read.

Link copied to clipboard
@get:JvmName(name = "priorResponse")
val priorResponse: Response?

Returns the response for the HTTP redirect or authorization challenge that triggered this response, or null if this response wasn't triggered by an automatic retry. The body of the returned response should not be read because it has already been consumed by the redirecting client.

Link copied to clipboard
@get:JvmName(name = "protocol")
val protocol: Protocol

Returns the HTTP protocol, such as Protocol.HTTP_1_1 or Protocol.HTTP_1_0.

Link copied to clipboard
@get:JvmName(name = "receivedResponseAtMillis")
val receivedResponseAtMillis: Long

Returns a timestamp taken immediately after OkHttp received this response's headers from the network. If this response is being served from the cache then this is the timestamp of the original response.

Link copied to clipboard
@get:JvmName(name = "request")
val request: Request

The request that initiated this HTTP response. This is not necessarily the same request issued by the application:

Link copied to clipboard
@get:JvmName(name = "sentRequestAtMillis")
val sentRequestAtMillis: Long

Returns a timestamp taken immediately before OkHttp transmitted the initiating request over the network. If this response is being served from the cache then this is the timestamp of the original request.

Functions

Link copied to clipboard

Returns the RFC 7235 authorization challenges appropriate for this response's code. If the response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. Otherwise this returns an empty list of challenges.

Link copied to clipboard
open override fun close()

Closes the response body. Equivalent to body().close().

Link copied to clipboard

Returns true if a Vary header contains an asterisk. Such responses cannot be cached.

Link copied to clipboard
fun header(name: String, defaultValue: String? = null): String?
Link copied to clipboard
fun headers(name: String): List<String>
Link copied to clipboard
Link copied to clipboard
fun peekBody(byteCount: Long): ResponseBody

Peeks up to byteCount bytes from the response body and returns them as a new response body. If fewer than byteCount bytes are in the response body, the full response body is returned. If more than byteCount bytes are in the response body, the returned value will be truncated to byteCount bytes.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard

Returns the trailers after the HTTP response, which may be empty. It is an error to call this before the entire HTTP response body has been consumed.

Link copied to clipboard

Returns the subset of the headers in this's request that impact the content of this's body.