Skip to content

//okhttp/okhttp3/Response

Response

[common]\ expect class Response : Closeable

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.

[jvm]\ actual class Response : Closeable

[nonJvm]\ actual class Response : Closeable

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

Name Summary
Builder [common, jvm, nonJvm]
[common]
expect open class Builder
[jvm, nonJvm]
actual open class Builder

Functions

Name Summary
challenges [jvm]
fun challenges(): List<Challenge>
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.
close [common, nonJvm]
[common]
expect open override fun close()
[nonJvm]
actual open override fun close()
Closes the response body. Equivalent to body().close().
[jvm]
actual open override fun close()
header [common, jvm, nonJvm]
[common]
expect fun header(name: String, defaultValue: String? = null): String?
[jvm]
@JvmOverloads
actual fun header(name: String, defaultValue: String? = null): String?
[nonJvm]
actual fun header(name: String, defaultValue: String? = null): String?
headers [common, jvm, nonJvm]
[common]
expect fun headers(name: String): List<String>
[jvm, nonJvm]
actual fun headers(name: String): List<String>
newBuilder [common, jvm, nonJvm]
[common]
expect fun newBuilder(): Response.Builder
[jvm, nonJvm]
actual fun newBuilder(): Response.Builder
peekBody [common, nonJvm]
[common]
expect fun peekBody(byteCount: Long): ResponseBody
[nonJvm]
actual 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.
[jvm]
actual fun peekBody(byteCount: Long): ResponseBody
toString [common, jvm, nonJvm]
[common]
expect open override fun toString(): String
[jvm, nonJvm]
actual open override fun toString(): String
trailers [common, jvm]
[common]
expect fun trailers(): Headers
[jvm]
actual fun trailers(): Headers
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.
[nonJvm]
actual fun trailers(): Headers

Properties

Name Summary
body [common]
expect 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.
[jvm]
@get:JvmName(name = "body")
actual val body: ResponseBody
[nonJvm]
actual 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.
cacheControl [common]
expect val cacheControl: CacheControl
Returns the cache control directives for this response. This is never null, even if this response contains no Cache-Control header.
[jvm]
@get:JvmName(name = "cacheControl")
actual val cacheControl: CacheControl
[nonJvm]
actual val cacheControl: CacheControl
Returns the cache control directives for this response. This is never null, even if this response contains no Cache-Control header.
cacheResponse [common]
expect 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.
[jvm]
@get:JvmName(name = "cacheResponse")
actual val cacheResponse: Response?
[nonJvm]
actual 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.
code [common]
expect val code: Int
Returns the HTTP status code.
[jvm]
@get:JvmName(name = "code")
actual val code: Int
[nonJvm]
actual val code: Int
Returns the HTTP status code.
handshake [jvm]
@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.
headers [common]
expect val headers: Headers
Returns the HTTP headers.
[jvm]
@get:JvmName(name = "headers")
actual val headers: Headers
Returns the HTTP headers.
[nonJvm]
actual val headers: Headers
Returns the HTTP headers.
isRedirect [common]
expect val isRedirect: Boolean
Returns true if this response redirects to another resource.
[jvm, nonJvm]
actual val isRedirect: Boolean
Returns true if this response redirects to another resource.
isSuccessful [common]
expect val isSuccessful: Boolean
Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.
[jvm]
actual val isSuccessful: Boolean
[nonJvm]
actual val isSuccessful: Boolean
Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.
message [common]
expect val message: String
Returns the HTTP status message.
[jvm]
@get:JvmName(name = "message")
actual val message: String
[nonJvm]
actual val message: String
Returns the HTTP status message.
networkResponse [common]
expect 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.
[jvm]
@get:JvmName(name = "networkResponse")
actual val networkResponse: Response?
[nonJvm]
actual 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.
priorResponse [common]
expect 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.
[jvm]
@get:JvmName(name = "priorResponse")
actual val priorResponse: Response?
[nonJvm]
actual 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.
protocol [common]
expect val protocol: Protocol
Returns the HTTP protocol, such as Protocol.HTTP_1_1 or Protocol.HTTP_1_0.
[jvm]
@get:JvmName(name = "protocol")
actual val protocol: Protocol
[nonJvm]
actual val protocol: Protocol
Returns the HTTP protocol, such as Protocol.HTTP_1_1 or Protocol.HTTP_1_0.
receivedResponseAtMillis [jvm]
@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.
request [common]
expect val request: Request
The request that initiated this HTTP response. This is not necessarily the same request issued by the application:
[jvm]
@get:JvmName(name = "request")
actual val request: Request
[nonJvm]
actual val request: Request
The request that initiated this HTTP response. This is not necessarily the same request issued by the application:
sentRequestAtMillis [jvm]
@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.

Extensions

Name Summary
hasVaryAll [jvm]
fun Response.hasVaryAll(): Boolean
Returns true if a Vary header contains an asterisk. Such responses cannot be cached.
varyHeaders [jvm]
fun Response.varyHeaders(): Headers
Returns the subset of the headers in this’s request that impact the content of this’s body.