跳到主要内容

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

ResponseType

Since: 1.0.0

Enumeration Members

NameTypeDefined in
3http.ts:74
1http.ts:72
2http.ts:73

Classes

Body

The body object to be used on POST and PUT requests.

Since: 1.0.0

Properties

payload

payload: unknown

Defined in: http.ts:95

type

type: string

Defined in: http.ts:94

Methods

bytes

Static bytes(bytes: Iterable<number> | ArrayBuffer | ArrayLike<number>): Body

Creates a new byte array body.

Example

import { Body } from "@tauri-apps/api/http"
Body.bytes(new Uint8Array([1, 2, 3]));

Parameters

NameTypeDescription
bytesIterable<number> | ArrayBuffer | ArrayLike<number>The body byte array.

Returns: Body

The body object ready to be used on the POST and PUT requests.

form

Static form(data: Record<string, Part> | FormData): Body

Creates a new form data body. The form data is an object where each key is the entry name, and the value is either a string or a file object.

By default it sets the application/x-www-form-urlencoded Content-Type header, but you can set it to multipart/form-data if the Cargo feature http-multipart is enabled.

Note that a file path must be allowed in the fs allowlist scope.

Example

import { Body } from "@tauri-apps/api/http"
const body = Body.form({
key: 'value',
image: {
file: '/path/to/file', // either a path or an array buffer of the file contents
mime: 'image/jpeg', // optional
fileName: 'image.jpg' // optional
}
});

// alternatively, use a FormData:
const form = new FormData();
form.append('key', 'value');
form.append('image', {
file: '/path/to/file',
mime: 'image/jpeg',
fileName: 'image.jpg'
});
const formBody = Body.form(form);

Parameters

NameTypeDescription
dataRecord<string, Part> | FormDataThe body data.

Returns: Body

The body object ready to be used on the POST and PUT requests.

json

Static json(data: Record<any, any>): Body

Creates a new JSON body.

Example

import { Body } from "@tauri-apps/api/http"
Body.json({
registered: true,
name: 'tauri'
});

Parameters

NameTypeDescription
dataRecord<any, any>The body JSON object.

Returns: Body

The body object ready to be used on the POST and PUT requests.

text

Static text(value: string): Body

Creates a new UTF-8 string body.

Example

import { Body } from "@tauri-apps/api/http"
Body.text('The body content as a string');

Parameters

NameTypeDescription
valuestringThe body string.

Returns: Body

The body object ready to be used on the POST and PUT requests.

Client

Since: 1.0.0

Properties

id

id: number

Defined in: http.ts:307

Methods

delete

delete<T>(url: string, options?: RequestOptions): Promise<Response<T>>

Makes a DELETE request.

Example

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.delete('http://localhost:3003/users/1');

Type parameters

  • T

Parameters

NameType
urlstring
options?RequestOptions

Returns: Promise<Response<T>>

drop

drop(): Promise<void>

Drops the client instance.

Example

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

Returns: Promise<void>

get

get<T>(url: string, options?: RequestOptions): Promise<Response<T>>

Makes a GET request.

Example

import { getClient, ResponseType } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.get('http://localhost:3003/users', {
timeout: 30,
// the expected response type
responseType: ResponseType.JSON
});

Type parameters

  • T

Parameters

NameType
urlstring
options?RequestOptions

Returns: Promise<Response<T>>

patch

patch<T>(url: string, options?: RequestOptions): Promise<Response<T>>

Makes a PATCH request.

Example

import { getClient, Body } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.patch('http://localhost:3003/users/1', {
body: Body.json({ email: 'contact@tauri.app' })
});

Type parameters

  • T

Parameters

NameType
urlstring
options?RequestOptions

Returns: Promise<Response<T>>

post

post<T>(url: string, body?: Body, options?: RequestOptions): Promise<Response<T>>

Makes a POST request.

Example

import { getClient, Body, ResponseType } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.post('http://localhost:3003/users', {
body: Body.json({
name: 'tauri',
password: 'awesome'
}),
// in this case the server returns a simple string
responseType: ResponseType.Text,
});

Type parameters

  • T

Parameters

NameType
urlstring
body?Body
options?RequestOptions

Returns: Promise<Response<T>>

put

put<T>(url: string, body?: Body, options?: RequestOptions): Promise<Response<T>>

Makes a PUT request.

Example

import { getClient, Body } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.put('http://localhost:3003/users/1', {
body: Body.form({
file: {
file: '/home/tauri/avatar.png',
mime: 'image/png',
fileName: 'avatar.png'
}
})
});

Type parameters

  • T

Parameters

NameType
urlstring
body?Body
options?RequestOptions

Returns: Promise<Response<T>>

request

request<T>(options: HttpOptions): Promise<Response<T>>

Makes an HTTP request.

Example

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.request({
method: 'GET',
url: 'http://localhost:3003/users',
});

Type parameters

  • T

Parameters

NameType
optionsHttpOptions

Returns: Promise<Response<T>>

Response<T>

Response object.

Since: 1.0.0

Type parameters

  • T

Properties

data

data: T

The response data.

Defined in: http.ts:290

headers

headers: Record<string, string>

The response headers.

Defined in: http.ts:286

ok

ok: boolean

A boolean indicating whether the response was successful (status in the range 200–299) or not.

Defined in: http.ts:284

rawHeaders

rawHeaders: Record<string, string[]>

The response raw headers.

Defined in: http.ts:288

status

status: number

The response status code.

Defined in: http.ts:282

url

url: string

The request URL.

Defined in: http.ts:280

Interfaces

ClientOptions

Since: 1.0.0

Properties

connectTimeout

Optional connectTimeout: number | Duration

Defines the maximum number of redirects the client should follow. If set to 0, no redirects will be followed.

Defined in: http.ts:65

maxRedirections

Optional maxRedirections: number

Defined in: http.ts:60

Duration

Since: 1.0.0

Properties

nanos

nanos: number

Defined in: http.ts:53

secs

secs: number

Defined in: http.ts:52

FilePart<T>

Since: 1.0.0

Type parameters

  • T

Properties

file

file: string | T

Defined in: http.ts:81

fileName

Optional fileName: string

Defined in: http.ts:83

mime

Optional mime: string

Defined in: http.ts:82

HttpOptions

Options object sent to the backend.

Since: 1.0.0

Properties

body

Optional body: Body

Defined in: http.ts:254

headers

Optional headers: Record<string, any>

Defined in: http.ts:252

method

method: HttpVerb

Defined in: http.ts:250

query

Optional query: Record<string, any>

Defined in: http.ts:253

responseType

Optional responseType: ResponseType

Defined in: http.ts:256

timeout

Optional timeout: number | Duration

Defined in: http.ts:255

url

url: string

Defined in: http.ts:251

Type Aliases

FetchOptions

FetchOptions: Omit<HttpOptions, "url">

Options for the fetch API.

Defined in: http.ts:262

HttpVerb

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

The request HTTP verb.

Defined in: http.ts:233

Part

Part: string | Uint8Array | FilePart<Uint8Array>

Defined in: http.ts:86

RequestOptions

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

Request options.

Defined in: http.ts:260

Functions

fetch

fetch<T>(url: string, options?: FetchOptions): 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

  • T

Parameters

NameType
urlstring
options?FetchOptions

Returns: Promise<Response<T>>

getClient

getClient(options?: ClientOptions): Promise<Client>

Creates a new client using the specified options.

Example

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

Since: 1.0.0

Parameters

NameTypeDescription
options?ClientOptionsClient configuration.

Returns: Promise<Client>

A promise resolving to the client instance.