Skip to main content
Version: 3.13.0

fetch()

The global fetch() method starts the process of fetching a resource from the network, returning a promise which is fulfilled once the response is available.

The promise resolves to the Response object representing the response to your request.

A fetch() promise only rejects when a network error is encountered (which is usually when there's a permissions issue or similar). A fetch() promise does not reject on HTTP errors (404, etc.). Instead, a then() handler must check the Response.ok and/or Response.status properties.

Note: The fetch() method's parameters are identical to those of the Request() constructor.

Syntax

fetch(resource)
fetch(resource, options)

Parameters

  • resource

    • : This defines the resource that you wish to fetch. This can either be:

      • A string or any other object with a "toString" method.
      • A Request object.
  • options optional

    • : An object containing any custom settings that you want to apply to the request. The possible options are:

      • method
        • : The request method, e.g., GET, POST.
      • headers
        • : Any headers you want to add to your request, contained within a Headers object or an object literal with String values.
      • body
        • : Any body that you want to add to your request: this can be an ArrayBuffer, a TypedArray, a DataView, a URLSearchParams, string object or literal, or a ReadableStream object.
      • backend Fastly-specific
        • Fastly-specific
      • cacheOverride Fastly-specific
      • cacheKey Fastly-specific
      • fastly Fastly-specific
        • decompressGzip: boolean optional
          • Whether to automatically gzip decompress the Response or not.

Return value

A Promise that resolves to a Response object.