Fetch
function fetch
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 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; } Return Type
Promise<Response> interface Body
Properties
body: ReadableStream<Uint8Array<ArrayBuffer>> | null A simple getter used to expose a ReadableStream of the body contents.
bodyUsed: boolean 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.
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.
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).
textStream(): ReadableStream<string> Takes a Response body stream and returns a ReadableStream<string> that streams the body decoded as UTF-8 text.
interface 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.
Properties
onerror: ((
this: EventSource,
ev: Event,
) => any) | null onmessage: ((
this: EventSource,
ev: MessageEvent,
) => any) | null onopen: ((
this: EventSource,
ev: Event,
) => any) | null readyState: number Returns the state of this EventSource object's connection. It can have the values described below.
url: string Returns the URL providing the event stream.
withCredentials: boolean 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 OPEN: 1 CLOSED: 2 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: (
this: EventSource,
ev: EventSourceEventMap[K],
) => any,
options?: boolean | AddEventListenerOptions,
): void addEventListener(
type: string,
listener: (
this: EventSource,
event: MessageEvent,
) => any,
options?: boolean | AddEventListenerOptions,
): void addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void removeEventListener<K extends keyof EventSourceEventMap>(
type: K,
listener: (
this: EventSource,
ev: EventSourceEventMap[K],
) => any,
options?: boolean | EventListenerOptions,
): void removeEventListener(
type: string,
listener: (
this: EventSource,
event: MessageEvent,
) => any,
options?: boolean | EventListenerOptions,
): void removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
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
See
interface EventSourceEventMap
interface EventSourceInit
Properties
withCredentials: boolean headers: HeadersInit 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
delete(name: string): void get(name: string): FormDataEntryValue | null getAll(name: string): FormDataEntryValue[] has(name: string): boolean interface 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.
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
This Fetch API interface represents a resource request.
Properties
cache: RequestCache Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching.
credentials: RequestCredentials 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.
destination: RequestDestination Returns the kind of resource requested by request, e.g., "document" or "script".
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 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]
keepalive: boolean Returns a boolean indicating whether or not request can outlive the global in which it was created.
method: string Returns request's HTTP method, which is "GET" by default.
mode: RequestMode 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.
redirect: RequestRedirect 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 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.
referrerPolicy: ReferrerPolicy Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer.
signal: AbortSignal 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 Returns the URL of request as a string.
Methods
interface RequestInit
Properties
cache: RequestCache A string indicating how the request will interact with the browser's cache to set request's cache.
credentials: RequestCredentials 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.
headers: HeadersInit A Headers object, an object literal, or an array of two-item arrays to set request's headers.
integrity: string A cryptographic hash of the resource to be fetched by request. Sets request's integrity.
keepalive: boolean A boolean to set request's keepalive.
method: string A string to set request's method.
mode: RequestMode A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.
priority: RequestPriority A string indicating the relative priority of the request. Sets request's priority.
redirect: RequestRedirect 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 A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.
referrerPolicy: ReferrerPolicy A referrer policy to set request's referrerPolicy.
signal: AbortSignal | null An AbortSignal to set request's signal.
window: any Can only be null. Used to disassociate request from any Window.
interface Response
This Fetch API interface represents the response to a request.
Properties
ok: boolean redirected: boolean status: number statusText: string type: ResponseType url: string Methods
interface ResponseInit
Properties
type alias BodyInit
Definition
Blob
| BufferSource
| FormData
| URLSearchParams
| ReadableStream<Uint8Array>
| Iterable<Uint8Array>
| AsyncIterable<Uint8Array>
| string
type alias FormDataEntryValue
Definition
File | string 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 RequestCredentials
Definition
"include"
| "omit"
| "same-origin"
type alias RequestDestination
Definition
""
| "audio"
| "audioworklet"
| "document"
| "embed"
| "font"
| "image"
| "manifest"
| "object"
| "paintworklet"
| "report"
| "script"
| "sharedworker"
| "style"
| "track"
| "video"
| "worker"
| "xslt"
type alias RequestInfo
Definition
Request | string type alias RequestMode
Definition
"cors"
| "navigate"
| "no-cors"
| "same-origin"
type alias RequestPriority
Definition
"auto"
| "high"
| "low"
type alias RequestRedirect
Definition
"error"
| "follow"
| "manual"
type alias ResponseType
Definition
"basic"
| "cors"
| "default"
| "error"
| "opaque"
| "opaqueredirect"
© 2018–2025 the Deno authors
Licensed under the MIT License.
https://docs.deno.com/api/web/fetch