Fetch - Deno documentation | Deno DocsSkip 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

Classes

c
Deno.HttpClient

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

Functions

f
Deno.createHttpClient

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

    Type Aliases

    T
    Deno.Proxy

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


      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 #

      #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 #

      Return Type #



      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.


      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; }

      Did you find what you needed?

      Privacy policy