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β
| Name | Type | Defined in |
|---|---|---|
3 | http.ts:74 | |
1 | http.ts:72 | |
2 | http.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β
Staticbytes(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
| Name | Type | Description |
|---|---|---|
bytes | Iterable<number> | ArrayBuffer | ArrayLike<number> | The body byte array. |
Returns: Body
The body object ready to be used on the POST and PUT requests.
formβ
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
| Name | Type | Description |
|---|---|---|
data | Record<string, Part> | FormData | The body data. |
Returns: Body
The body object ready to be used on the POST and PUT requests.
jsonβ
Creates a new JSON body.
Example
import { Body } from "@tauri-apps/api/http"
Body.json({
registered: true,
name: 'tauri'
});
Parameters
| Name | Type | Description |
|---|---|---|
data | Record<any, any> | The body JSON object. |
Returns: Body
The body object ready to be used on the POST and PUT requests.
textβ
Statictext(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
| Name | Type | Description |
|---|---|---|
value | string | The 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:303
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
| Name | Type |
|---|---|
url | string |
options? | RequestOptions |
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
| Name | Type |
|---|---|
url | string |
options? | RequestOptions |
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
| Name | Type |
|---|---|
url | string |
options? | RequestOptions |
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
| Name | Type |
|---|---|
url | string |
body? | Body |
options? | RequestOptions |
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
| Name | Type |
|---|---|
url | string |
body? | Body |
options? | RequestOptions |
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
| Name | Type |
|---|---|
options | HttpOptions |
Response<T>β
Response object.
Since: 1.0.0
Type parameters
T
Propertiesβ
dataβ
data:
T
The response data.
Defined in: http.ts:286
headersβ
headers:
Record<string,string>
The response headers.
Defined in: http.ts:282
okβ
ok:
boolean
A boolean indicating whether the response was successful (status in the range 200β299) or not.
Defined in: http.ts:280
rawHeadersβ
rawHeaders:
Record<string,string[]>
The response raw headers.
Defined in: http.ts:284
statusβ
status:
number
The response status code.
Defined in: http.ts:278
urlβ
url:
string
The request URL.
Defined in: http.ts:276
Interfacesβ
ClientOptionsβ
Since: 1.0.0
Propertiesβ
connectTimeoutβ
OptionalconnectTimeout:number|Duration
Defined in: http.ts:65
maxRedirectionsβ
OptionalmaxRedirections:number
Defines the maximum number of redirects the client should follow. If set to 0, no redirects will be followed.
Defined in: http.ts:64
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β
OptionalfileName:string
Defined in: http.ts:83
mimeβ
Optionalmime:string
Defined in: http.ts:82
HttpOptionsβ
Options object sent to the backend.
Since: 1.0.0
Propertiesβ
bodyβ
Optionalbody:Body
Defined in: http.ts:250
headersβ
Optionalheaders:Record<string,any>
Defined in: http.ts:248
methodβ
method:
HttpVerb
Defined in: http.ts:246
queryβ
Optionalquery:Record<string,any>
Defined in: http.ts:249
responseTypeβ
OptionalresponseType:ResponseType
Defined in: http.ts:252
timeoutβ
Optionaltimeout:number|Duration
Defined in: http.ts:251
urlβ
url:
string
Defined in: http.ts:247
Type Aliasesβ
FetchOptionsβ
FetchOptions:
Omit<HttpOptions,"url">
Options for the fetch API.
Defined in: http.ts:258
HttpVerbβ
HttpVerb:
"GET"|"POST"|"PUT"|"DELETE"|"PATCH"|"HEAD"|"OPTIONS"|"CONNECT"|"TRACE"
The request HTTP verb.
Defined in: http.ts:229
Partβ
Part:
string|Uint8Array|FilePart<Uint8Array>
Defined in: http.ts:86
RequestOptionsβ
RequestOptions:
Omit<HttpOptions,"method"|"url">
Request options.
Defined in: http.ts:256
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
| Name | Type |
|---|---|
url | string |
options? | FetchOptions |
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
| Name | Type | Description |
|---|---|---|
options? | ClientOptions | Client configuration. |
A promise resolving to the client instance.