wix-fetch.WixFetchResponse

wix-fetch.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.

Usage

ZmV0Y2godXJsLCBbb3B0aW9uc10pCiAgLnRoZW4oIChodHRwUmVzcG9uc2UpID0+IHsKICAgIGlmIChodHRwUmVzcG9uc2Uub2spIHsKICAgICAgLy8gLi4uIEhUVFAgc3RhdHVzIGNvZGUgaW4gcmFuZ2UgMnh4CiAgICB9CiAgICBlbHNlIHsKICAgICAgLy8gLi4uIEhUVFAgc3RhdHVzIGNvZGUgaW4gcmFuZ2UgM3h4LCA0eHgsIG9yIDV4eAogICAgICAvLyAgICAgKG9yIGFueSBvdGhlciByZXNwb25zZSBjb2RlKQogICAgfQogIH0gKQogIC5jYXRjaCggKGVycm9yKSA9PiB7IC4uLiB9ICk7
fetch(url, [options])
  .then( (httpResponse) => {
    if (httpResponse.ok) {
      // ... HTTP status code in range 2xx
    }
    else {
      // ... HTTP status code in range 3xx, 4xx, or 5xx
      //     (or any other response code)
    }
  } )
  .catch( (error) => { ... } );

See Also

wix-fetch

Contents

bodyUsed Indicates whether the body of the response has been used yet.
headers The response headers.
ok Indicates if the request was successful, meaning its status is in the range 2xx.
status The reponse status code.
statusText The response status message.
url The response URL.
json( ) Reads the response body as JSON.
text( ) Reads the response body as a string.
bodyUsed

bodyUsed

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

Syntax

get bodyUsed(): Boolean

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.

Type

Boolean

Examples

Get whether the response's body has been used.

bGV0IHdhc0JvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOyAvLyBmYWxzZQo=
let wasBodyUsed = httpResponse.bodyUsed; // false

Get a resource and the response information

aW1wb3J0IHtmZXRjaH0gZnJvbSAnd2l4LWZldGNoJzsKCi8vIC4uLgoKZmV0Y2goImh0dHBzOi8vZXhjaGFuZ2VyYXRlc2FwaS5pby9hcGkvbGF0ZXN0Iiwge21ldGhvZDogImdldCJ9KQogIC50aGVuKCAoaHR0cFJlc3BvbnNlKSA9PiB7CiAgICBsZXQgdXJsID0gaHR0cFJlc3BvbnNlLnVybDsKICAgIGxldCBzdGF0dXNDb2RlID0gaHR0cFJlc3BvbnNlLnN0YXR1czsKICAgIGxldCBzdGF0dXNUZXh0ID0gaHR0cFJlc3BvbnNlLnN0YXR1c1RleHQ7CiAgICBsZXQgaGVhZGVycyA9IGh0dHBSZXNwb25zZS5oZWFkZXJzOwogICAgbGV0IGJvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOwogICAgaWYgKGh0dHBSZXNwb25zZS5vaykgewogICAgICByZXR1cm4gaHR0cFJlc3BvbnNlLmpzb24oKTsKICAgIH0KICAgIGVsc2UgewogICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoIkZldGNoIGRpZCBub3Qgc3VjY2VlZCIpOwogICAgfQogIH0gKQogIC50aGVuKCAoanNvbikgPT4gewogICAgY29uc29sZS5sb2coanNvbi5yYXRlc1siVVNEIl0pOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import {fetch} from 'wix-fetch';

// ...

fetch("https://exchangeratesapi.io/api/latest", {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.rates["USD"]);
  } )
  .catch( (err) => {
    console.log(err);
  } );
headers

headers

The response headers.

Syntax

get headers(): Object

Type

Object

Examples

Get the response's headers.

bGV0IGhlYWRlcnMgPSBodHRwUmVzcG9uc2UuaGVhZGVyOwo=
let headers = httpResponse.header;

Get a resource and the response information

aW1wb3J0IHtmZXRjaH0gZnJvbSAnd2l4LWZldGNoJzsKCi8vIC4uLgoKZmV0Y2goImh0dHBzOi8vZXhjaGFuZ2VyYXRlc2FwaS5pby9hcGkvbGF0ZXN0Iiwge21ldGhvZDogImdldCJ9KQogIC50aGVuKCAoaHR0cFJlc3BvbnNlKSA9PiB7CiAgICBsZXQgdXJsID0gaHR0cFJlc3BvbnNlLnVybDsKICAgIGxldCBzdGF0dXNDb2RlID0gaHR0cFJlc3BvbnNlLnN0YXR1czsKICAgIGxldCBzdGF0dXNUZXh0ID0gaHR0cFJlc3BvbnNlLnN0YXR1c1RleHQ7CiAgICBsZXQgaGVhZGVycyA9IGh0dHBSZXNwb25zZS5oZWFkZXJzOwogICAgbGV0IGJvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOwogICAgaWYgKGh0dHBSZXNwb25zZS5vaykgewogICAgICByZXR1cm4gaHR0cFJlc3BvbnNlLmpzb24oKTsKICAgIH0KICAgIGVsc2UgewogICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoIkZldGNoIGRpZCBub3Qgc3VjY2VlZCIpOwogICAgfQogIH0gKQogIC50aGVuKCAoanNvbikgPT4gewogICAgY29uc29sZS5sb2coanNvbi5yYXRlc1siVVNEIl0pOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import {fetch} from 'wix-fetch';

// ...

fetch("https://exchangeratesapi.io/api/latest", {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.rates["USD"]);
  } )
  .catch( (err) => {
    console.log(err);
  } );
ok

ok

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

Syntax

get ok(): Boolean

Type

Boolean

Examples

Get whether a response is ok.

bGV0IGlzT2sgPSBodHRwUmVzcG9uc2Uub2s7IC8vIHRydWUK
let isOk = httpResponse.ok; // true

Get a resource and the response information

aW1wb3J0IHtmZXRjaH0gZnJvbSAnd2l4LWZldGNoJzsKCi8vIC4uLgoKZmV0Y2goImh0dHBzOi8vZXhjaGFuZ2VyYXRlc2FwaS5pby9hcGkvbGF0ZXN0Iiwge21ldGhvZDogImdldCJ9KQogIC50aGVuKCAoaHR0cFJlc3BvbnNlKSA9PiB7CiAgICBsZXQgdXJsID0gaHR0cFJlc3BvbnNlLnVybDsKICAgIGxldCBzdGF0dXNDb2RlID0gaHR0cFJlc3BvbnNlLnN0YXR1czsKICAgIGxldCBzdGF0dXNUZXh0ID0gaHR0cFJlc3BvbnNlLnN0YXR1c1RleHQ7CiAgICBsZXQgaGVhZGVycyA9IGh0dHBSZXNwb25zZS5oZWFkZXJzOwogICAgbGV0IGJvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOwogICAgaWYgKGh0dHBSZXNwb25zZS5vaykgewogICAgICByZXR1cm4gaHR0cFJlc3BvbnNlLmpzb24oKTsKICAgIH0KICAgIGVsc2UgewogICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoIkZldGNoIGRpZCBub3Qgc3VjY2VlZCIpOwogICAgfQogIH0gKQogIC50aGVuKCAoanNvbikgPT4gewogICAgY29uc29sZS5sb2coanNvbi5yYXRlc1siVVNEIl0pOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import {fetch} from 'wix-fetch';

// ...

fetch("https://exchangeratesapi.io/api/latest", {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.rates["USD"]);
  } )
  .catch( (err) => {
    console.log(err);
  } );
status

status

The reponse status code.

Syntax

get status(): Number

Type

Number

Examples

Get the response's status code.

bGV0IHN0YXR1c0NvZGUgPSBodHRwUmVzcG9uc2Uuc3RhdHVzOyAvLyAyMDAK
let statusCode = httpResponse.status; // 200

Get a resource and the response information

aW1wb3J0IHtmZXRjaH0gZnJvbSAnd2l4LWZldGNoJzsKCi8vIC4uLgoKZmV0Y2goImh0dHBzOi8vZXhjaGFuZ2VyYXRlc2FwaS5pby9hcGkvbGF0ZXN0Iiwge21ldGhvZDogImdldCJ9KQogIC50aGVuKCAoaHR0cFJlc3BvbnNlKSA9PiB7CiAgICBsZXQgdXJsID0gaHR0cFJlc3BvbnNlLnVybDsKICAgIGxldCBzdGF0dXNDb2RlID0gaHR0cFJlc3BvbnNlLnN0YXR1czsKICAgIGxldCBzdGF0dXNUZXh0ID0gaHR0cFJlc3BvbnNlLnN0YXR1c1RleHQ7CiAgICBsZXQgaGVhZGVycyA9IGh0dHBSZXNwb25zZS5oZWFkZXJzOwogICAgbGV0IGJvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOwogICAgaWYgKGh0dHBSZXNwb25zZS5vaykgewogICAgICByZXR1cm4gaHR0cFJlc3BvbnNlLmpzb24oKTsKICAgIH0KICAgIGVsc2UgewogICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoIkZldGNoIGRpZCBub3Qgc3VjY2VlZCIpOwogICAgfQogIH0gKQogIC50aGVuKCAoanNvbikgPT4gewogICAgY29uc29sZS5sb2coanNvbi5yYXRlc1siVVNEIl0pOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import {fetch} from 'wix-fetch';

// ...

fetch("https://exchangeratesapi.io/api/latest", {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.rates["USD"]);
  } )
  .catch( (err) => {
    console.log(err);
  } );
statusText

statusText

The response status message.

Syntax

get statusText(): String

Type

String

Examples

Get the response's status message.

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

Get a resource and the response information

aW1wb3J0IHtmZXRjaH0gZnJvbSAnd2l4LWZldGNoJzsKCi8vIC4uLgoKZmV0Y2goImh0dHBzOi8vZXhjaGFuZ2VyYXRlc2FwaS5pby9hcGkvbGF0ZXN0Iiwge21ldGhvZDogImdldCJ9KQogIC50aGVuKCAoaHR0cFJlc3BvbnNlKSA9PiB7CiAgICBsZXQgdXJsID0gaHR0cFJlc3BvbnNlLnVybDsKICAgIGxldCBzdGF0dXNDb2RlID0gaHR0cFJlc3BvbnNlLnN0YXR1czsKICAgIGxldCBzdGF0dXNUZXh0ID0gaHR0cFJlc3BvbnNlLnN0YXR1c1RleHQ7CiAgICBsZXQgaGVhZGVycyA9IGh0dHBSZXNwb25zZS5oZWFkZXJzOwogICAgbGV0IGJvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOwogICAgaWYgKGh0dHBSZXNwb25zZS5vaykgewogICAgICByZXR1cm4gaHR0cFJlc3BvbnNlLmpzb24oKTsKICAgIH0KICAgIGVsc2UgewogICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoIkZldGNoIGRpZCBub3Qgc3VjY2VlZCIpOwogICAgfQogIH0gKQogIC50aGVuKCAoanNvbikgPT4gewogICAgY29uc29sZS5sb2coanNvbi5yYXRlc1siVVNEIl0pOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import {fetch} from 'wix-fetch';

// ...

fetch("https://exchangeratesapi.io/api/latest", {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.rates["USD"]);
  } )
  .catch( (err) => {
    console.log(err);
  } );
url

url

The response URL.

Syntax

get url(): String

Type

String

Examples

Get the response's url.

bGV0IHVybCA9IGh0dHBSZXNwb25zZS51cmw7IC8vICJodHRwczovL2V4Y2hhbmdlcmF0ZXNhcGkuaW8vYXBpL2xhdGVzdCIK
let url = httpResponse.url; // "https://exchangeratesapi.io/api/latest"

Get a resource and the response information

aW1wb3J0IHtmZXRjaH0gZnJvbSAnd2l4LWZldGNoJzsKCi8vIC4uLgoKZmV0Y2goImh0dHBzOi8vZXhjaGFuZ2VyYXRlc2FwaS5pby9hcGkvbGF0ZXN0Iiwge21ldGhvZDogImdldCJ9KQogIC50aGVuKCAoaHR0cFJlc3BvbnNlKSA9PiB7CiAgICBsZXQgdXJsID0gaHR0cFJlc3BvbnNlLnVybDsKICAgIGxldCBzdGF0dXNDb2RlID0gaHR0cFJlc3BvbnNlLnN0YXR1czsKICAgIGxldCBzdGF0dXNUZXh0ID0gaHR0cFJlc3BvbnNlLnN0YXR1c1RleHQ7CiAgICBsZXQgaGVhZGVycyA9IGh0dHBSZXNwb25zZS5oZWFkZXJzOwogICAgbGV0IGJvZHlVc2VkID0gaHR0cFJlc3BvbnNlLmJvZHlVc2VkOwogICAgaWYgKGh0dHBSZXNwb25zZS5vaykgewogICAgICByZXR1cm4gaHR0cFJlc3BvbnNlLmpzb24oKTsKICAgIH0KICAgIGVsc2UgewogICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoIkZldGNoIGRpZCBub3Qgc3VjY2VlZCIpOwogICAgfQogIH0gKQogIC50aGVuKCAoanNvbikgPT4gewogICAgY29uc29sZS5sb2coanNvbi5yYXRlc1siVVNEIl0pOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import {fetch} from 'wix-fetch';

// ...

fetch("https://exchangeratesapi.io/api/latest", {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.rates["USD"]);
  } )
  .catch( (err) => {
    console.log(err);
  } );
json( )

json( )

Reads the response body as JSON.

function json(): Promise<Object>

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.

Return Value

Returns a Promise

On fulfillment Object The parsed response body as JSON.
On rejection Error The errors that caused the rejection.

Examples

Get the response's body as JSON.

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

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

ZmV0Y2godXJsLCB7bWV0aG9kOiAnZ2V0J30pCiAgLnRoZW4ocmVzcG9uc2UgPT4gaHR0cFJlc3BvbnNlLmpzb24oKSkKICAudGhlbihqc29uID0+IGNvbnNvbGUubG9nKEpTT04uc3RyaW5naWZ5KGpzb24pKSk7Cg==
fetch(url, {method: 'get'})
  .then(response => httpResponse.json())
  .then(json => console.log(JSON.stringify(json)));
text( )

text( )

Reads the response body as a string.

function text(): Promise<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.

Return Value

Returns a Promise

On fulfillment String The response body as a string.
On rejection Error The errors that caused the rejection.

Examples

Get the response's body as text.

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

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

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