FetchResponse

interface FetchResponse

Fetch: Response.

Describes the structure of an HTTP response provided via the Fetch sub-system, which implements the WhatWG Fetch API Standard, in both browser JavaScript engines and server-side JavaScript engines. The Fetch system and specification are designed to make HTTP data fetches easy in JavaScript environments, and stand as a modern replacement for the famous XMLHttpRequest API in browsers, and the http/https layers in NodeJS.

Fetch responses model normal HTTP response structures, including a status code/status text, response headers, and a response body, as applicable. Not all HTTP responses carry a response body. Responses can be used to model content originating from a guest VM script (in the case of a generated or synthesized response, potentially in response to input, such as an HTTP request), or used to model a response to originated requests, for example, when fetching data from inside a VM and accessing the resulting response.

From MDN: "The Response interface of the Fetch API represents the response to a request. You can create a new Response object using the Response() constructor, but you are more likely to encounter a Response object being returned as the result of another API operation—for example, a service worker FetchEvent.respondWith, or a simple fetch()."

 

Use as execution output

The FetchResponse interface is used, along with the FetchRequest interface, to model server-style invocation for the JavaScript VM. In this case, a FetchResponse instance is expected as the output for a VM execution run, which is seeded with a FetchRequest instance provided by the outer host server runtime.

Multiple concrete FetchResponse implementations exist; each originate from a given server implementation.

 

Use from a guest context

Responses can be created from within a JavaScript guest, or received from within a guest context when executing a fetched FetchRequest (host permissions permitting).

Inheritors

Types

Link copied to clipboard
object Defaults

Default values applied to FetchResponse interfaces.

Link copied to clipboard

Describes the layout of constructors available to create new FetchResponse implementation objects, either via host-side code or, where supported, via guest-side code. Some of these constructors are driven by spec requirements whereas others are provided for host-side convenience. See individual constructor docs for more details.

Properties

Link copied to clipboard
abstract val body: ReadableStream?

Provides access to a ReadableStream which expresses body data enclosed with this response, as applicable and where supported; if no stream can be produced for this response, null may be returned.

Link copied to clipboard
abstract val bodyUsed: Boolean

Indicates whether the ReadableStream provided by body has been consumed for this request.

Link copied to clipboard
abstract val headers: FetchHeaders

Provides a typed view of headers associated with this HTTP response, via FetchHeaders, which behaves as a specialized JavaScript-compatible multi-map. Multiple header values can be expressed per header key, if desired, which can be safely combined into a comma-separated header value group upon render.

Link copied to clipboard
open val ok: Boolean

Indicates whether this response terminated with an "ok" status. If the response terminated with an HTTP status which was between 200 and 299, inclusive, then this returns true. For statuses 400 and above, this value is always false.

Link copied to clipboard

Indicates whether this response was redirected.

Link copied to clipboard
open val status: Int

Provides the HTTP status code which this response terminated with. HTTP status codes are typically expressed from the various HTTP specifications, although custom codes are possible.

Link copied to clipboard
open val statusText: String

Provides the terminal HTTP status text which corresponds with the terminal status code for this response.

Link copied to clipboard
open val type: String

Provides the type of response expressed by this object. Available options:

Link copied to clipboard
open val url: String

Provides the URL which produced this response, in absolute form, at the final resting destination for the response (factoring in redirects, as applicable).