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


class Deno.HttpClient

implements Disposable

A custom HttpClient for use with fetch function. This is designed to allow custom certificates or proxies to be used with fetch().

Examples

const caCert = await Deno.readTextFile("./ca.pem");
const client = Deno.createHttpClient({ caCerts: [ caCert ] });
const req = await fetch("https://myserver.com", { client });

Methods

[Symbol.dispose](): void
close(): void

Close the HTTP client.


function Deno.createHttpClient

createHttpClient(options: CreateHttpClientOptions | (CreateHttpClientOptions & TlsCertifiedKeyPem)): HttpClient

Create a custom HttpClient to use with fetch. This is an extension of the web platform Fetch API which allows Deno to use custom TLS CA certificates and connect via a proxy while using fetch().

The cert and key options can be used to specify a client certificate and key to use when connecting to a server that requires client authentication (mutual TLS or mTLS). The cert and key options must be provided in PEM format.

Examples

const caCert = await Deno.readTextFile("./ca.pem");
const client = Deno.createHttpClient({ caCerts: [ caCert ] });
const response = await fetch("https://myserver.com", { client });
const client = Deno.createHttpClient({
  proxy: { url: "http://myproxy.com:8080" }
});
const response = await fetch("https://myserver.com", { client });
const key = "----BEGIN PRIVATE KEY----...";
const cert = "----BEGIN CERTIFICATE----...";
const client = Deno.createHttpClient({ key, cert });
const response = await fetch("https://myserver.com", { client });

Parameters

options: CreateHttpClientOptions | (CreateHttpClientOptions & TlsCertifiedKeyPem)

Return Type

HttpClient

interface Deno.BasicAuth

Basic authentication credentials to be used with a Deno.Proxy server when specifying Deno.CreateHttpClientOptions.

Properties

username: string

The username to be used against the proxy server.

password: string

The password to be used against the proxy server.


interface Deno.CreateHttpClientOptions

The options used when creating a Deno.HttpClient.

Properties

caCerts: string[]
optional

A list of root certificates that will be used in addition to the default root certificates to verify the peer's certificate.

Must be in PEM format.

proxy: Proxy
optional

An alternative transport (a proxy) to use for new connections.

poolMaxIdlePerHost: number
optional

Sets the maximum number of idle connections per host allowed in the pool.

poolIdleTimeout: number | false
optional

Set an optional timeout for idle sockets being kept-alive. Set to false to disable the timeout.

http1: boolean = true
optional

Whether HTTP/1.1 is allowed or not.

http2: boolean = true
optional

Whether HTTP/2 is allowed or not.

allowHost: boolean = false
optional

Whether setting the host header is allowed or not.

localAddress: string
optional

Sets the local address where the socket will connect from.

http2MaxHeaderListSize: number
optional

Sets the max HTTP/2 header list size (in bytes) that the client will accept. This maps to the SETTINGS_MAX_HEADER_LIST_SIZE HTTP/2 setting.

If not set, the default value from the underlying HTTP library is used.


type alias Deno.Proxy

The definition for alternative transports (or proxies) in Deno.CreateHttpClientOptions.

Supported proxies:

  • HTTP/HTTPS proxy: this uses passthrough to tunnel HTTP requests, or HTTP CONNECT to tunnel HTTPS requests through a different server.
  • SOCKS5 proxy: this uses the SOCKS5 protocol to tunnel TCP connections through a different server.
  • TCP socket: this sends all requests to a specified TCP socket.
  • Unix domain socket: this sends all requests to a local Unix domain socket rather than a TCP socket. Not supported on Windows.
  • Vsock socket: this sends all requests to a local vsock socket. Only supported on Linux and macOS.

Definition

{ transport?:
"http"
| "https"
| "socks5"
; url: string; basicAuth?: BasicAuth; }

| { transport: "tcp"; hostname: string; port: number; }
| { transport: "unix"; path: string; }
| { transport: "vsock"; cid: number; port: number; }

© 2018–2025 the Deno authors
Licensed under the MIT License.
https://docs.deno.com/api/deno/fetch

Spec-Zone.ru

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