Files
archived-tauri-docs/docs/api/js/modules/http.md
2022-07-15 12:00:05 +02:00

3.8 KiB

@tauri-apps/api / http

Module: http

Access the HTTP client written in Rust.

This package is also accessible with window.__TAURI__.http when build.withGlobalTauri in tauri.conf.json is set to true.

The APIs must be allowlisted on tauri.conf.json:

{
  "tauri": {
    "allowlist": {
      "http": {
        "all": true, // enable all http APIs
        "request": true // enable HTTP request API
      }
    }
  }
}

It is recommended to allowlist only the APIs you use for optimal bundle size and security.

Security

This API has a scope configuration that forces you to restrict the URLs and paths that can be accessed using glob patterns.

For instance, this scope configuration only allows making HTTP requests to the GitHub API for the tauri-apps organization:

{
  "tauri": {
    "allowlist": {
      "http": {
        "scope": ["https://api.github.com/repos/tauri-apps/*"]
      }
    }
  }
}

Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.

Enumerations

Classes

Interfaces

Type Aliases

FetchOptions

FetchOptions: Omit<HttpOptions, "url">

Options for the fetch API.

Defined in

http.ts:213


HttpVerb

HttpVerb: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE"

The request HTTP verb.

Defined in

http.ts:188


Part

Part: string | Uint8Array | FilePart<Uint8Array>

Defined in

http.ts:70


RequestOptions

RequestOptions: Omit<HttpOptions, "method" | "url">

Request options.

Defined in

http.ts:211

Functions

fetch

fetch<T>(url, options?): Promise<Response<T>>

Perform an HTTP request using the default client.

Example

import { fetch } from '@tauri-apps/api/http';
const response = await fetch('http://localhost:3003/users/2', {
  method: 'GET',
  timeout: 30,
});

Type parameters

Name
T

Parameters

Name Type Description
url string The request URL.
options? FetchOptions The fetch options.

Returns

Promise<Response<T>>

The response object.


getClient

getClient(options?): Promise<Client>

Creates a new client using the specified options.

Example

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();

Parameters

Name Type Description
options? ClientOptions Client configuration.

Returns

Promise<Client>

A promise resolving to the client instance.