v1.2.0.1-beta1

Description

The Response interface of the Fetch API represents the response to a request.

A Response type is returned when a Net.fetch() method promise is fulfilled, either directly in case of a successful result (passed to then() handler), or as a member of any DOMException thrown as a result of request errors (passed to catch() handler). See the local Net.fetch() documentation for details.

Properties

bool async
 
any body
 
bool bodyUsed
 
Headers headers
 
bool ok
 
bool redirected
 
string responseType
 
int status
 
string statusText
 
URL url
 
XMLHttpRequest xhr
 

Public Member Functions

< Promise|ArrayBufferarrayBuffer ()
 
Promise bufferAsync ()
 
ArrayBuffer bufferSync ()
 
< Promise|string > base64 ()
 
Promise base64Async ()
 
string base64Sync ()
 
any bodyAs (string type)
 
< Promise|Object|Array > json ()
 
Promise jsonAsync ()
 
< Object|Array > jsonSync ()
 
< Promise|string > text ()
 
Promise textAsync ()
 
string textSync ()
 
< Promise|XMLDocument > xml ()
 
Promise xmlAsync ()
 
XMLDocument xmlSync ()
 
Response clone ()
 
Error blob ()
 

Property Documentation

◆ async

bool async
read

Indicates whether the original request was made asynchronously or not.

Typically this means whether fetch() (async = true) or request() (async = false) was used to initiate the transaction.

The value determines the result type from the type-specific access methods text()/json()/arrayBuffer()/base64()/xml(); a Promise is returned from these methods when async is true, and the actual requested data type when async is false.

To explicitly get a Promise or immediate result from those access methods, use the *Sync() or *Async() versions directly. Eg. jsonSync() and jsonAsync().

This property is an alias for xhr.async.

See also
xhr

◆ body

any body
read

Returns XMLHttpRequest.response.

The return type will vary based on the responseType property set on the original Request (an ArrayBuffer, parsed JSON object/array, XML Document, or plain text).

See also
bodyAs(), getText(), getJson(), getXml(), getBuffer()

◆ bodyUsed

bool bodyUsed
read

Returns true if response body has been parsed into a specific format (by calling body or any of the result-specific promise methods like json(), arrayBuffer(), etc).

This doesn't carry much actual significance (since it is not a data stream), but is retained for compatibility with standard.

◆ headers

Headers headers
read

The Headers object associated with the response.

See also
https://developer.mozilla.org/en-US/docs/Web/API/Headers

◆ ok

bool ok
read

A boolean indicating whether the response was successful (status in the range 200 – 299) or not.

◆ redirected

bool redirected
read

Indicates whether or not the response is the result of a redirect (that is, its URL list has more than one entry).

See also
url

◆ responseType

string responseType
readwrite

This possible values of this property correspond to the Request.responseType property values (both are aliases for XMLHttpRequest.responseType).

The value dictates what type of object the body property will be returned as (text, json, xml, or binary/raw). Initially this is set to whatever was set in the original Request (or fetch() options), if anything (default is blank, equivalent to "text" type). However it can also be changed after the response is received in order to affect the result type returned by body. For example based on received headers.

See also
bodyAs() for a convenience shortcut method.

◆ status

bool status
read

The HTTP status code of the response (eg.

200 for a success, 404 not found, etc).

◆ statusText

string statusText
read

The status message corresponding to the status code (eg.

"OK" for 200).

◆ url

URL url
read

The URL of the response.

If the request was redirected, this may not be the same as was requested.

See also
redirected

◆ xhr

XMLHttpRequest xhr
read

This is the underlying XMLHttpRequest object which actually handled the request.

All properties of the result can be read directly from this object if desired, w/out processing further promises (as with "typical" fetch() usage). For example xhr.response or xhr.responseText can be processed immediately in the first then() handler, while the usual text(), json(), etc, access methods all return further async promises for chaining.

Member Function Documentation

◆ arrayBuffer()

< Promise|ArrayBuffer > arrayBuffer ( )
Returns

arrayBuffer() is also aliased as simply buffer().

◆ bufferAsync()

Promise bufferAsync ( )

Returns a Promise that resolves with an ArrayBuffer representation of the response body.

◆ bufferSync()

ArrayBuffer bufferSync ( )

Returns an ArrayBuffer representation of the response body.

Same as bodyAs("arraybuffer").

◆ base64()

< Promise|string > base64 ( )
Returns

◆ base64Async()

Promise base64Async ( )

Returns a Promise that resolves with an ArrayBuffer representation of the response body but encoded as a Base-64 string.

Convenience method equivalent to

fetch(...).then(r => r.arrayBuffer()).then((data) => { var b64data = btoa(data); ... })

to shorten to

fetch(...).then(r => r.base64()).then((b64data) => { ... })`;

◆ base64Sync()

string base64Sync ( )

Returns an ArrayBuffer representation of the response body but encoded as a Base-64 string.

◆ bodyAs()

any bodyAs ( string  type)

Returns the response body as the given type.

The values for type are the same as for the Request.responseType property. This is equivalent to: result.responseType = type; var data = result.body;

Note
For "json" and "xml" types, no validation of the result is done before attempting to parse the result data as the specified type. If the data is malformed or not of the requested type at all, an exception may the thrown by the parser handling the data. To ensure the result is in a specific format, first check for appropriate response header(s) before calling this method.

◆ json()

< Promise|Object|Array > json ( )
Returns

◆ jsonAsync()

Promise jsonAsync ( )

Returns a Promise that resolves with the result of parsing the response body text as JSON (either a JS Object or an Array).

Note
No validation of the result type is done before attempting to parse the result data as JSON. If the data is malformed or not JSON at all, an exception will the thrown by the parser (same as would be with JSON.parse()). To ensure the result is in JSON format, first check for appropriate response header(s) before calling this method.

◆ jsonSync()

< Object|Array > jsonSync ( )

Returns body as the result of parsing the response body text as JSON (either a JS Object or an Array).

Same as bodyAs("json").

◆ text()

< Promise|string > text ( )
Returns

◆ textAsync()

Promise textAsync ( )

Returns a Promise that resolves with a text representation of the response body.

◆ textSync()

string textSync ( )

Returns a text representation of the response body.

Same as bodyAs("text").

◆ xml()

< Promise|XMLDocument > xml ( )
Returns

◆ xmlAsync()

Promise xmlAsync ( )

Returns a Promise that resolves with an XML Document type representation of the response body.

See XMLHttpRequest for details about the available Document properties.

Note
No validation of the result type is done before attempting to parse the result data as XML. If the data is malformed or not XML at all, an exception will the thrown by the XML parser. To ensure the result is in XML format, first check for appropriate response header(s) before calling this method.

◆ xmlSync()

XMLDocument xmlSync ( )

Returns an XMLDocument type representation of the response body.

Same as bodyAs("xml"). See XMLHttpRequest for details about the available Document properties.

◆ clone()

Response clone ( )

Creates a clone (deep copy) of this Response object.

Note that the underlying XMLHttpRequest object (available via the xhr property) is still shared across copies. Generally cloning a Response is not recommended.

◆ blob()

Error blob ( )

Since Blob type is not supported in the environment, this method throws a TypeError.

Retained for compatibility with standard.