Response object

The Response object is used to represent the response of a request.

Syntax

 var response = new Response(body, init); 

Members

The Response object has these types of members:

  • Methods
  • Properties

Methods

The Response object has these methods.

Method Description
arrayBuffer

Takes a Response or Request stream and returns a promise for an ArrayBuffer object.

blob

Takes a Response or Request stream and returns a promise for a Blob object.

clone

Creates a copy of the current Response object.

json

Takes a Response or Request stream and returns a promise for an object containing JSON data.

text

Takes a Response or Request stream and returns a promise for a USVString object.

 

Properties

The Response object has these properties.

Property Access type Description

body

Read-only

Contains the stream associated with the Response object.

bodyUsed

Read-only

Determines if the body is non-null and its stream is distributed.

headers

Read-only

Contains the Headers object of the response.

ok

Read-only

Determines if the response was successful.

status

Read-only

Contains the status code of the response.

statusText

Read-only

Contains the status message that corresponds to the status code of the response.

type

Read-only

Contains the type of the response.

url

Read-only

Contains the URL of the response.

 

Standards information

Remarks

The first optional parameter of the Response object is used to define the body of the response (Blob, BufferSource, FormData, or USVString). The body is set to be null by default.

The second optional parameter of the Response object is an options object. Possible values for the options are specified in the ResponseInit dictionary.

Dictionary of ResponseInit members:

Dictionary Member Type Description
status short Set to 200 by default.
statusText ByteString Set to "OK" by default.
headers HeadersInit A body to be added to the request.

 

ResponseType enumeration:

Type Description
"basic" The header list excludes any headers in internal response's header list whose name is a forbidden response-header name.
"cors" The header list excludes any headers in internal response's header list whose name is not a CORS-safelisted response-header name, given internal response's CORS-exposed header-name list.
"default" The default type unless specified otherwise.
"error" The response is a network error. A network error is a response whose status is always 0, status message is always the empty byte sequence, header list is always empty, body is always null, and cache state is always "none".
"opaque" The url list is the empty list, status is 0, status message is the empty byte sequence, header list is the empty list, body is null, and cache state is "none".
"opaqueredirect" The status is 0, status message is the empty byte sequence, header list is the empty list, body is null, and cache state is "none".

 

The getReader method of the ReadableStream object returns a ReadableStreamReader object that can be used to stream a response. With this object, buffering large responses is more efficient when compared to XHR which buffers the whole request.