CodeAPI

WixFetchResponse

An object returned by the fetch() function representing an HTTP response to a fetch.

The WixFetchResponse object contains information about an HTTP response. More functionality is available in each of the respective Fetch API implementations.

Table of Contents

PROPERTIES

?
Store values associated with an object.
bodyUsedIndicates whether the body of the response has been used yet.
headersThe response headers.
okIndicates if the request was successful, meaning its status is in the range 2xx.
statusThe reponse status code.
urlThe response URL.

FUNCTIONS

?
Perform actions on an object.
json( )Reads the response body as JSON.
text( )Reads the response body as a string.

bodyUsed

Indicates whether the body of the response has been used yet.

Description

The response's body is considered used once it has been read. For example, after calling the response's json() function, the value of bodyUsed will be true.

Syntax

get bodyUsed(): boolean
TYPE
?
The kind of data the property stores.
boolean

Examples

Get whether the response's body has been used.

let wasBodyUsed = httpResponse.bodyUsed; // false

Get a resource and the response information

import {fetch} from 'wix-fetch';

// ...

fetch("https://someapi.com/api/someendpoint", {"method": "get"})
  .then( (httpResponse) => {
    let url = httpResponse.url;
    let statusCode = httpResponse.status;
    let statusText = httpResponse.statusText;
    let headers = httpResponse.headers;
    let bodyUsed = httpResponse.bodyUsed;
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then( (json) => {
    console.log(json.someKey);
  } )
  .catch( (err) => {
    console.log(err);
  } );

headers

The response headers.

Syntax

get headers(): Object
TYPE
?
The kind of data the property stores.
Object

Examples

Get the response's headers.

let headers = httpResponse.header;

Get a resource and the response information

import {fetch} from 'wix-fetch';

// ...

fetch("https://someapi.com/api/someendpoint", {"method": "get"})
  .then( (httpResponse) => {
    let url = httpResponse.url;
    let statusCode = httpResponse.status;
    let statusText = httpResponse.statusText;
    let headers = httpResponse.headers;
    let bodyUsed = httpResponse.bodyUsed;
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then( (json) => {
    console.log(json.someKey);
  } )
  .catch( (err) => {
    console.log(err);
  } );

ok

Indicates if the request was successful, meaning its status is in the range 2xx.

Syntax

get ok(): boolean
TYPE
?
The kind of data the property stores.
boolean

Examples

Get whether a response is ok.

let isOk = httpResponse.ok; // true

Get a resource and the response information

import {fetch} from 'wix-fetch';

// ...

fetch("https://someapi.com/api/someendpoint", {"method": "get"})
  .then( (httpResponse) => {
    let url = httpResponse.url;
    let statusCode = httpResponse.status;
    let statusText = httpResponse.statusText;
    let headers = httpResponse.headers;
    let bodyUsed = httpResponse.bodyUsed;
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then( (json) => {
    console.log(json.someKey);
  } )
  .catch( (err) => {
    console.log(err);
  } );

status

The reponse status code.

Syntax

get status(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the response's status code.

let statusCode = httpResponse.status; // 200

Get a resource and the response information

import {fetch} from 'wix-fetch';

// ...

fetch("https://someapi.com/api/someendpoint", {"method": "get"})
  .then( (httpResponse) => {
    let url = httpResponse.url;
    let statusCode = httpResponse.status;
    let statusText = httpResponse.statusText;
    let headers = httpResponse.headers;
    let bodyUsed = httpResponse.bodyUsed;
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then( (json) => {
    console.log(json.someKey);
  } )
  .catch( (err) => {
    console.log(err);
  } );

statusText

The response status message.

Syntax

get statusText(): string
TYPE
?
The kind of data the property stores.
string

Examples

Get the response's status message.

let statusText = httpResponse.statusText; // "OK"

Get a resource and the response information

import {fetch} from 'wix-fetch';

// ...

fetch("https://someapi.com/api/someendpoint", {"method": "get"})
  .then( (httpResponse) => {
    let url = httpResponse.url;
    let statusCode = httpResponse.status;
    let statusText = httpResponse.statusText;
    let headers = httpResponse.headers;
    let bodyUsed = httpResponse.bodyUsed;
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then( (json) => {
    console.log(json.someKey);
  } )
  .catch( (err) => {
    console.log(err);
  } );

url

The response URL.

Syntax

get url(): string
TYPE
?
The kind of data the property stores.
string

Examples

Get the response's url.

let url = httpResponse.url; // "https://someapi.com/api/someendpoint"

Get a resource and the response information

import {fetch} from 'wix-fetch';

// ...

fetch("https://someapi.com/api/someendpoint", {"method": "get"})
  .then( (httpResponse) => {
    let url = httpResponse.url;
    let statusCode = httpResponse.status;
    let statusText = httpResponse.statusText;
    let headers = httpResponse.headers;
    let bodyUsed = httpResponse.bodyUsed;
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    else {
      return Promise.reject("Fetch did not succeed");
    }
  } )
  .then( (json) => {
    console.log(json.someKey);
  } )
  .catch( (err) => {
    console.log(err);
  } );

json( )

Reads the response body as JSON.

Description

The json() function reads the body response stream to its completion and parses it as JSON. It returns a Promise that resolves to the response body as a JSON object.

Syntax

function json(): Promise<Object>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<Object>

Fulfilled - The parsed response body as JSON. Rejected - The errors that caused the rejection.

Examples

Get the response's body as JSON.

let json = response.json(); // {"key": "some value"}

Get the response's body as JSON and log it to the console.

fetch(url, {"method": "get"})
  .then(response => response.json())
  .then(json => console.log(JSON.stringify(json)));

text( )

Reads the response body as a string.

Description

The text() function reads the body response stream to its completion and formats the data as a string. It returns a Promise that resolves to the response body as a string.

Syntax

function text(): Promise<string>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<string>

Fulfilled - The response body as a string. Rejected - The errors that caused the rejection.

Examples

Get the response's body as text.

let text = httpResponse.text(); // {"key": "some value"}

Get the response's body as text and log it to the console.

fetch(url, {method: 'get'})
  .then(httpResponse => httpResponse.text())
  .then(text => console.log(text));