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:139
type
β
type:
string
Defined in: http.ts:138
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
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 | FormInput | 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
β
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
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:316
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:299
headers
β
headers:
Record
<string
,string
>
The response headers.
Defined in: http.ts:295
ok
β
ok:
boolean
A boolean indicating whether the response was successful (status in the range 200β299) or not.
Defined in: http.ts:293
rawHeaders
β
rawHeaders:
Record
<string
,string
[]>
The response raw headers.
Defined in: http.ts:297
status
β
status:
number
The response status code.
Defined in: http.ts:291
url
β
url:
string
The request URL.
Defined in: http.ts:289
Interfacesβ
ClientOptions
β
Since: 1.0.0
Propertiesβ
connectTimeout
β
Optional
connectTimeout:number
|Duration
Defined in: http.ts:65
maxRedirections
β
Optional
maxRedirections: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
β
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:263
headers
β
Optional
headers:Record
<string
,any
>
Defined in: http.ts:261
method
β
method:
HttpVerb
Defined in: http.ts:259
query
β
Optional
query:Record
<string
,any
>
Defined in: http.ts:262
responseType
β
Optional
responseType:ResponseType
Defined in: http.ts:265
timeout
β
Optional
timeout:number
|Duration
Defined in: http.ts:264
url
β
url:
string
Defined in: http.ts:260
Type Aliasesβ
FetchOptions
β
FetchOptions:
Omit
<HttpOptions
,"url"
>
Options for the fetch
API.
Defined in: http.ts:271
FormInput
β
Defined in: http.ts:88
HttpVerb
β
HttpVerb:
"GET"
|"POST"
|"PUT"
|"DELETE"
|"PATCH"
|"HEAD"
|"OPTIONS"
|"CONNECT"
|"TRACE"
The request HTTP verb.
Defined in: http.ts:242
Part
β
Part:
string
|Uint8Array
|FilePart
<Uint8Array
>
Defined in: http.ts:86
RequestOptions
β
RequestOptions:
Omit
<HttpOptions
,"method"
|"url"
>
Request options.
Defined in: http.ts:269
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.