http
访问用Rust编写的HTTP客户端。
当 tauri.conf.json
中的 build.withGlobalTauri
设置为 true
时,此包也可以通过 window.__TAURI__.http 访问。
这些API必须在 tauri.conf.json
中列入允许列表:
{ "tauri": { "allowlist": { "http": { "all": true, // enable all http APIs "request": true // enable HTTP request API } } } }
建议仅列入您使用的API,以获得最佳的包大小和安全性。
Security
此API有一个作用域配置,强制您使用全局模式限制可访问的URL和路径。
例如,此作用域配置仅允许对tauri-apps组织的GitHub API进行HTTP请求:
{
"tauri": {
"allowlist": {
"http": {
"scope": ["https://api.github.com/repos/tauri-apps/*"]
}
}
}
}
尝试执行任何未在作用域中配置的URL的API将导致因拒绝访问而承诺被拒绝。
Classes
Body
用于POST和PUT请求的主体对象。
方法
-
Static
bytes(bytes
:Iterable
<number
> |ArrayBuffer
|ArrayLike
<number
>):Body
创建一个新的字节数组主体。
示例
import { Body } from "@tauri-apps/api/http"
Body.bytes(new Uint8Array([1, 2, 3]));
参数
名称 |
类型 |
描述 |
|
|
请求体字节数组。 |
返回: Body
主体对象已准备好用于POST和PUT请求。
创建一个新的表单数据主体。表单数据是一个对象,其中每个键是条目名称,值是字符串或文件对象。
默认情况下,它会设置 application/x-www-form-urlencoded
内容类型头,但如果启用了Cargo功能 http-multipart
,您可以将其设置为 multipart/form-data
。
注意,文件路径必须在 fs
允许列表范围内。
示例
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);
参数
名称 |
类型 |
描述 |
|
请求体数据。 |
返回: Body
主体对象已准备好用于POST和PUT请求。
创建一个新的JSON请求体。
示例
import { Body } from "@tauri-apps/api/http"
Body.json({
registered: true,
name: 'tauri'
});
参数
名称 |
类型 |
描述 |
|
|
请求体JON对象。 |
主体对象已准备好用于POST和PUT请求。
-
Static
text(value
:string
):Body
创建一个新的UTF-8字符串请求体。
示例
import { Body } from "@tauri-apps/api/http"
Body.text('The body content as a string');
参数
名称 |
类型 |
描述 |
|
|
请求体字符串。 |
返回: Body
主体对象已准备好用于POST和PUT请求。
Client
方法
-
delete<
T
>(url
:string
,options?
:RequestOptions
):Promise
<Response
<T
>>
发送DELETE请求。
示例
import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.delete('http://localhost:3003/users/1');
类型参数
-
T
参数
名称 |
类型 |
|
|
|
-
drop():
Promise
<void
>
销毁客户端实例。
示例
import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
await client.drop();
返回: Promise
<void
>
-
get<
T
>(url
:string
,options?
:RequestOptions
):Promise
<Response
<T
>>
发送GET请求。
示例
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
});
类型参数
-
T
参数
名称 |
类型 |
|
|
|
-
patch<
T
>(url
:string
,options?
:RequestOptions
):Promise
<Response
<T
>>
发送PATCH请求。
示例
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' })
});
类型参数
-
T
参数
名称 |
类型 |
|
|
|
-
post<
T
>(url
:string
,body?
:Body
,options?
:RequestOptions
):Promise
<Response
<T
>>
发送POST请求。
示例
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,
});
类型参数
-
T
参数
名称 |
类型 |
|
|
|
|
|
-
put<
T
>(url
:string
,body?
:Body
,options?
:RequestOptions
):Promise
<Response
<T
>>
发送PUT请求。
示例
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'
}
})
});
类型参数
-
T
参数
名称 |
类型 |
|
|
|
|
|
-
request<
T
>(options
:HttpOptions
):Promise
<Response
<T
>>
发起一个HTTP请求。
示例
import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.request({
method: 'GET',
url: 'http://localhost:3003/users',
});
类型参数
-
T
参数
名称 |
类型 |
|
Response<T>
响应对象。
属性
-
data:
T
响应数据
定义在: http.ts:286
-
headers:
Record
<string
,string
>
响应头。
定义在: http.ts:282
-
ok:
boolean
一个布尔值,指示响应是否成功(状态在200-299范围内)。
定义在: http.ts:280
-
rawHeaders:
Record
<string
,string
[]>
响应的原始头信息。
定义在: http.ts:284
-
status:
number
响应状态码。
定义在: http.ts:278
-
url:
string
请求URL。
定义在: http.ts:276
Interfaces
ClientOptions
Since: 1.0.0
属性
-
Optional
connectTimeout:number
|Duration
定义在: http.ts:65
-
Optional
maxRedirections:number
定义客户端应跟随的最大重定向次数。如果设置为0,则不会跟随任何重定向。
定义在: http.ts:64
FilePart<T>
属性
-
file:
string
|T
定义在: http.ts:81
-
Optional
fileName:string
定义在: http.ts:83
-
Optional
mime:string
定义在: http.ts:82
HttpOptions
发送到后端的选项对象。
属性
-
Optional
body:Body
定义在: http.ts:250
-
Optional
headers:Record
<string
,any
>
定义在: http.ts:248
-
method:
HttpVerb
定义在: http.ts:246
-
Optional
query:Record
<string
,any
>
定义在: http.ts:249
-
Optional
responseType:ResponseType
定义在: http.ts:252
-
Optional
timeout:number
|Duration
定义在: http.ts:251
-
url:
string
定义在: http.ts:247
Type Aliases
HttpVerb
-
HttpVerb:
"GET"
|"POST"
|"PUT"
|"DELETE"
|"PATCH"
|"HEAD"
|"OPTIONS"
|"CONNECT"
|"TRACE"
请求的HTTP动词。
定义在: http.ts:229
Functions
fetch
-
fetch<
T
>(url
:string
,options?
:FetchOptions
):Promise
<Response
<T
>>
使用默认客户端执行HTTP请求。
示例
import { fetch } from '@tauri-apps/api/http';
const response = await fetch('http://localhost:3003/users/2', {
method: 'GET',
timeout: 30,
});
getClient
-
getClient(
options?
:ClientOptions
):Promise
<Client
>
使用指定的选项创建一个新的客户端。
示例
import { getClient } from '@tauri-apps/api/http';
const client = await getClient();