Skip to content
Tauri
Releases

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

Binary
Binary: 3;

Source: http.ts:74


JSON
JSON: 1;

Source: http.ts:72


Text
Text: 2;

Source: http.ts:73

Classes

Body

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

Since

1.0.0

Properties

PropertyType
payloadunknown
typestring

Methods

bytes()
static bytes(bytes): Body

Creates a new byte array body.

Example
import { Body } from '@tauri-apps/api/http';
Body.bytes(new Uint8Array([1, 2, 3]));
Parameters
ParameterTypeDescription
bytesIterable< number > | ArrayBuffer | ArrayLike< number >The body byte array.
Returns

Body

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

Source: http.ts:230


form()
static form(data): 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, 'image.png');
const formBody = Body.form(form);
Parameters
ParameterTypeDescription
dataFormInputThe body data.
Returns

Body

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

Source: http.ts:179


json()
static json(data): Body

Creates a new JSON body.

Example
import { Body } from '@tauri-apps/api/http';
Body.json({
registered: true,
name: 'tauri',
});
Parameters
ParameterTypeDescription
dataRecord< any, any >The body JSON object.
Returns

Body

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

Source: http.ts:198


text()
static text(value): 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
ParameterTypeDescription
valuestringThe body string.
Returns

Body

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

Source: http.ts:214


Client

Since

1.0.0

Properties

PropertyType
idnumber

Methods

delete()
delete<T>(url, options?): 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
Parameter
T
Parameters
ParameterType
urlstring
options?RequestOptions
Returns

Promise< Response< T > >

Source: http.ts:502


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 >

Source: http.ts:331


get()
get<T>(url, options?): 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
Parameter
T
Parameters
ParameterType
urlstring
options?RequestOptions
Returns

Promise< Response< T > >

Source: http.ts:407


patch()
patch<T>(url, options?): 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
Parameter
T
Parameters
ParameterType
urlstring
options?RequestOptions
Returns

Promise< Response< T > >

Source: http.ts:485


post()
post<T>(
url,
body?,
options?): 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
Parameter
T
Parameters
ParameterType
urlstring
body?Body
options?RequestOptions
Returns

Promise< Response< T > >

Source: http.ts:431


put()
put<T>(
url,
body?,
options?): 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
Parameter
T
Parameters
ParameterType
urlstring
body?Body
options?RequestOptions
Returns

Promise< Response< T > >

Source: http.ts:461


request()
request<T>(options): 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
Parameter
T
Parameters
ParameterType
optionsHttpOptions
Returns

Promise< Response< T > >

Source: http.ts:353


Response

Response object.

Since

1.0.0

Type parameters

Parameter
T

Properties

PropertyTypeDescription
dataTThe response data.
headersRecord< string, string >The response headers.
okbooleanA boolean indicating whether the response was successful (status in the range 200–299) or not.
rawHeadersRecord< string, string[] >The response raw headers.
statusnumberThe response status code.
urlstringThe request URL.

Interfaces

ClientOptions

Since

1.0.0

Properties

PropertyTypeDescription
connectTimeout?number | Duration-
maxRedirections?numberDefines the maximum number of redirects the client should follow.
If set to 0, no redirects will be followed.

Duration

Since

1.0.0

Properties

PropertyType
nanosnumber
secsnumber

FilePart

Since

1.0.0

Type parameters

Parameter
T

Properties

PropertyType
filestring | T
fileName?string
mime?string

HttpOptions

Options object sent to the backend.

Since

1.0.0

Properties

PropertyType
body?Body
headers?Record< string, any >
methodHttpVerb
query?Record< string, any >
responseType?ResponseType
timeout?number | Duration
urlstring

Type Aliases

FetchOptions

FetchOptions: Omit<HttpOptions, 'url'>;

Options for the fetch API.

Source: http.ts:271


FormInput

FormInput: Record<string, Part> | FormData;

Source: http.ts:88


HttpVerb

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

The request HTTP verb.

Source: http.ts:242


Part

Part: string | Uint8Array | FilePart<Uint8Array>;

Source: http.ts:86


RequestOptions

RequestOptions: Omit<HttpOptions, 'method' | 'url'>;

Request options.

Source: http.ts:269

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

Parameter
T

Parameters

ParameterType
urlstring
options?FetchOptions

Returns

Promise< Response< T > >

Source: http.ts:549


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

Since

1.0.0

Parameters

ParameterTypeDescription
options?ClientOptionsClient configuration.

Returns

Promise< Client >

A promise resolving to the client instance.

Source: http.ts:525


© 2024 Tauri Contributors. CC-BY / MIT