Skip to main content

Fetch

HTTP client for fetching data across a network. Retrieve resources from servers, handle responses, and manage network requests.

Eg fetch, Response, Request, Headers

Functions

f
fetch

Fetch a resource from the network. It returns a Promise that resolves to the Response to that Request, whether it is successful or not.

    Interfaces

    I
    v
    EventSource

    Represents a connection to a server that sends server-sent events, receiving updates pushed by the server as a stream of message events over a persistent HTTP connection that automatically reconnects when interrupted.

    I
    EventSourceEventMap
    No documentation available
    I
    EventSourceInit
    No documentation available
    I
    v
    FormData

    Provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".

    I
    v
    Headers

    This Fetch API interface allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing. A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs. You can add to this using methods like append() (see Examples). In all methods of this interface, header names are matched by case-insensitive byte sequence.

    I
    v
    Response

    This Fetch API interface represents the response to a request.

    I
    ResponseInit
    No documentation available

    Type Aliases

    T
    BodyInit
    No documentation available
      T
      FormDataEntryValue
      No documentation available
        T
        HeadersInit
        No documentation available
          T
          ReferrerPolicy
          No documentation available
            T
            RequestCache
            No documentation available
              T
              RequestCredentials
              No documentation available
                T
                RequestDestination
                No documentation available
                  T
                  RequestInfo
                  No documentation available
                    T
                    RequestMode
                    No documentation available
                      T
                      RequestPriority
                      No documentation available
                        T
                        RequestRedirect
                        No documentation available
                          T
                          ResponseType
                          No documentation available

                            function fetch

                            allow-net
                            allow-read

                            Overload 1

                            #fetch(
                            input: RequestInfo | URL,
                            init?: RequestInit,
                            ): Promise<Response>

                            Fetch a resource from the network. It returns a Promise that resolves to the Response to that Request, whether it is successful or not.

                            const response = await fetch("http://my.json.host/data.json");
                            console.log(response.status);  // e.g. 200
                            console.log(response.statusText); // e.g. "OK"
                            const jsonData = await response.json();
                            

                            Parameters #

                            #input: RequestInfo | URL
                            #init: RequestInit
                            optional

                            Return Type #

                            Promise<Response>

                            Overload 2

                            #fetch(
                            input: RequestInfo | URL,
                            init?: RequestInit & { client?: Deno.HttpClient; },
                            ): Promise<Response>

                            The Fetch API which also supports setting a Deno.HttpClient which provides a way to connect via proxies and use custom TLS certificates.

                            Parameters #

                            #input: RequestInfo | URL
                            #init: RequestInit & { client?: Deno.HttpClient; }
                            optional

                            Return Type #

                            Promise<Response>

                            interface Body

                            Properties #

                            #body: ReadableStream<Uint8Array<ArrayBuffer>> | null
                            readonly

                            A simple getter used to expose a ReadableStream of the body contents.

                            #bodyUsed: boolean
                            readonly

                            Stores a Boolean that declares whether the body has been used in a response yet.

                            Methods #

                            #arrayBuffer(): Promise<ArrayBuffer>

                            Takes a Response stream and reads it to completion. It returns a promise that resolves with an ArrayBuffer.

                            #blob(): Promise<Blob>

                            Takes a Response stream and reads it to completion. It returns a promise that resolves with a Blob.

                            #bytes(): Promise<Uint8Array<ArrayBuffer>>

                            Takes a Response stream and reads it to completion. It returns a promise that resolves with a Uint8Array.

                            #formData(): Promise<FormData>

                            Takes a Response stream and reads it to completion. It returns a promise that resolves with a FormData object.

                            #json(): Promise<any>

                            Takes a Response stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as JSON.

                            #text(): Promise<string>

                            Takes a Response stream and reads it to completion. It returns a promise that resolves with a USVString (text).


                            interface EventSource

                            extends EventTarget

                            Represents a connection to a server that sends server-sent events, receiving updates pushed by the server as a stream of message events over a persistent HTTP connection that automatically reconnects when interrupted.

                            Properties #

                            #onerror: ((
                            ev: Event,
                            ) => any) | null
                            #onmessage: (() => any) | null
                            #onopen: ((
                            ev: Event,
                            ) => any) | null
                            #readyState: number
                            readonly

                            Returns the state of this EventSource object's connection. It can have the values described below.

                            #url: string
                            readonly

                            Returns the URL providing the event stream.

                            #withCredentials: boolean
                            readonly

                            Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise.

                            #CONNECTING: 0
                            readonly
                            #OPEN: 1
                            readonly
                            #CLOSED: 2
                            readonly

                            Methods #

                            #close(): void

                            Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED.

                            #addEventListener<K extends keyof EventSourceEventMap>(
                            type: K,
                            listener: () => any,
                            options?: boolean | AddEventListenerOptions,
                            ): void
                            #addEventListener(
                            type: string,
                            listener: () => any,
                            options?: boolean | AddEventListenerOptions,
                            ): void
                            #addEventListener(
                            type: string,
                            options?: boolean | AddEventListenerOptions,
                            ): void
                            #removeEventListener<K extends keyof EventSourceEventMap>(
                            type: K,
                            listener: () => any,
                            options?: boolean | EventListenerOptions,
                            ): void
                            #removeEventListener(
                            type: string,
                            listener: () => any,
                            options?: boolean | EventListenerOptions,
                            ): void
                            #removeEventListener(
                            type: string,
                            options?: boolean | EventListenerOptions,
                            ): void

                            variable EventSource

                            The EventSource interface is a web content's interface to server-sent events. An EventSource instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format. The connection remains open until closed by calling EventSource.close.

                            Properties #

                            #CONNECTING: 0
                            readonly
                            #OPEN: 1
                            readonly
                            #CLOSED: 2
                            readonly

                            See #




                            interface FormData

                            Provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".

                            Methods #

                            #append(
                            name: string,
                            value: string | Blob,
                            fileName?: string,
                            ): void
                            #delete(name: string): void
                            #get(name: string): FormDataEntryValue | null
                            #getAll(name: string): FormDataEntryValue[]
                            #has(name: string): boolean
                            #set(
                            name: string,
                            value: string | Blob,
                            fileName?: string,
                            ): void

                            variable FormData

                            Provides a way to construct a set of key/value pairs representing form fields and their values, which can then be sent using the fetch API. It uses the same format a form would use if the encoding type were set to "multipart/form-data".

                            Properties #

                            See #


                            interface Headers

                            extends DomIterable<string, string>

                            This Fetch API interface allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing. A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs. You can add to this using methods like append() (see Examples). In all methods of this interface, header names are matched by case-insensitive byte sequence.

                            Methods #

                            #append(
                            name: string,
                            value: string,
                            ): void

                            Appends a new value onto an existing header inside a Headers object, or adds the header if it does not already exist.

                            #delete(name: string): void

                            Deletes a header from a Headers object.

                            #get(name: string): string | null

                            Returns a ByteString sequence of all the values of a header within a Headers object with a given name.

                            #has(name: string): boolean

                            Returns a boolean stating whether a Headers object contains a certain header.

                            #set(
                            name: string,
                            value: string,
                            ): void

                            Sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist.

                            #getSetCookie(): string[]

                            Returns an array containing the values of all Set-Cookie headers associated with a response.

                            variable Headers

                            This Fetch API interface allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing. A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs. You can add to this using methods like append() (see Examples). In all methods of this interface, header names are matched by case-insensitive byte sequence.

                            Properties #


                            interface Request

                            extends Body

                            This Fetch API interface represents a resource request.

                            Properties #

                            Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching.

                            Returns the credentials mode associated with request, which is a string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL.

                            Returns the kind of resource requested by request, e.g., "document" or "script".

                            #headers: Headers
                            readonly

                            Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.

                            #integrity: string
                            readonly

                            Returns request's subresource integrity metadata, which is a cryptographic hash of the resource being fetched. Its value consists of multiple hashes separated by whitespace. [SRI]

                            #isHistoryNavigation: boolean
                            readonly

                            Returns a boolean indicating whether or not request is for a history navigation (a.k.a. back-forward navigation).

                            #isReloadNavigation: boolean
                            readonly

                            Returns a boolean indicating whether or not request is for a reload navigation, e.g. a refresh triggered via the browser's reload control or by calling location.reload().

                            #keepalive: boolean
                            readonly

                            Returns a boolean indicating whether or not request can outlive the global in which it was created.

                            #method: string
                            readonly

                            Returns request's HTTP method, which is "GET" by default.

                            #mode: RequestMode
                            readonly

                            Returns the mode associated with request, which is a string indicating whether the request will use CORS, or will be restricted to same-origin URLs.

                            Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default.

                            #referrer: string
                            readonly

                            Returns the referrer of request. Its value can be a same-origin URL if explicitly set in init, the empty string to indicate no referrer, and "about:client" when defaulting to the global's default. This is used during fetching to determine the value of the Referer header of the request being made.

                            Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer.

                            Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler.

                            #url: string
                            readonly

                            Returns the URL of request as a string.

                            Methods #

                            variable Request

                            This Fetch API interface represents a resource request.

                            Properties #


                            interface RequestInit

                            Properties #

                            #body: BodyInit | null
                            optional

                            A BodyInit object or null to set request's body.

                            A string indicating how the request will interact with the browser's cache to set request's cache.

                            A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials.

                            A Headers object, an object literal, or an array of two-item arrays to set request's headers.

                            #integrity: string
                            optional

                            A cryptographic hash of the resource to be fetched by request. Sets request's integrity.

                            #keepalive: boolean
                            optional

                            A boolean to set request's keepalive.

                            #method: string
                            optional

                            A string to set request's method.

                            #mode: RequestMode
                            optional

                            A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.

                            A string indicating the relative priority of the request. Sets request's priority.

                            A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.

                            #referrer: string
                            optional

                            A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.

                            A referrer policy to set request's referrerPolicy.

                            #signal: AbortSignal | null
                            optional

                            An AbortSignal to set request's signal.

                            #window: any
                            optional

                            Can only be null. Used to disassociate request from any Window.


                            interface Response

                            extends Body

                            This Fetch API interface represents the response to a request.

                            Properties #

                            #headers: Headers
                            readonly
                            #ok: boolean
                            readonly
                            #redirected: boolean
                            readonly
                            #status: number
                            readonly
                            #statusText: string
                            readonly
                            #url: string
                            readonly

                            Methods #

                            variable Response

                            This Fetch API interface represents the response to a request.

                            Properties #

                            Methods #

                            #json(
                            data: unknown,
                            init?: ResponseInit,
                            ): Response
                            #redirect(
                            url: string | URL,
                            status?: number,
                            ): Response




                            type alias HeadersInit

                            Definition #

                            Iterable<string[]> | Record<string, string>

                            type alias ReferrerPolicy

                            Definition #

                            ""
                            | "no-referrer"
                            | "no-referrer-when-downgrade"
                            | "origin"
                            | "origin-when-cross-origin"
                            | "same-origin"
                            | "strict-origin"
                            | "strict-origin-when-cross-origin"
                            | "unsafe-url"

                            type alias RequestCache

                            Definition #

                            "default"
                            | "force-cache"
                            | "no-cache"
                            | "no-store"
                            | "only-if-cached"
                            | "reload"


                            type alias RequestDestination

                            Definition #

                            ""
                            | "audio"
                            | "audioworklet"
                            | "document"
                            | "embed"
                            | "font"
                            | "image"
                            | "manifest"
                            | "object"
                            | "paintworklet"
                            | "report"
                            | "script"
                            | "sharedworker"
                            | "style"
                            | "track"
                            | "video"
                            | "worker"
                            | "xslt"


                            type alias RequestMode

                            Definition #

                            "cors"
                            | "navigate"
                            | "no-cors"
                            | "same-origin"



                            type alias ResponseType

                            Definition #

                            "basic"
                            | "cors"
                            | "default"
                            | "error"
                            | "opaque"
                            | "opaqueredirect"

                            Did you find what you needed?

                            Privacy policy