Spec-Zone.ru › Deno 2

Fetch

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

Eg fetch, Response, Request, Headers


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).

textStream(): ReadableStream<string>

Takes a Response body stream and returns a ReadableStream<string> that streams the body decoded as UTF-8 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: ((
this: EventSource,
ev: Event,
) => any) | null
onmessage: ((
this: EventSource,
ev: MessageEvent,
) => any) | null
onopen: ((
this: EventSource,
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: (
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

prototype: EventSource
CONNECTING: 0
readonly
OPEN: 1
readonly
CLOSED: 2
readonly

See

  • https://developer.mozilla.org/docs/Web/API/EventSource


interface EventSourceEventMap

Properties

error: Event
message: MessageEvent
open: Event

interface EventSourceInit

Properties

withCredentials: boolean
optional
headers: HeadersInit
optional

interface FormData

extends DomIterable<string, FormDataEntryValue>

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

prototype: FormData
readonly

See

  • https://developer.mozilla.org/docs/Web/API/FormData


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

prototype: Headers
readonly

interface Request

extends Body

This Fetch API interface represents a resource request.

Properties

cache: RequestCache
readonly

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
readonly

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
readonly

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.

redirect: RequestRedirect
readonly

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.

referrerPolicy: ReferrerPolicy
readonly

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

signal: AbortSignal
readonly

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

clone(): Request

variable Request

This Fetch API interface represents a resource request.

Properties

prototype: Request
readonly

interface RequestInit

Properties

body: BodyInit | null
optional

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

cache: RequestCache
optional

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

credentials: RequestCredentials
optional

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
optional

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.

priority: RequestPriority
optional

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

redirect: RequestRedirect
optional

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.

referrerPolicy: ReferrerPolicy
optional

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
type: ResponseType
readonly
url: string
readonly

Methods

clone(): Response

variable Response

This Fetch API interface represents the response to a request.

Properties

prototype: Response
readonly

Methods

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

interface ResponseInit

Properties

headers: HeadersInit
optional
status: number
optional
statusText: string
optional

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API