跳至主要內容

HTTP

存取以 Rust 編寫的 HTTP 應用程式。

tauri.conf.json 中的 build.withGlobalTauri 設為 true 時,也可以透過 window.__TAURI__.http 存取這個套件。

必須在 tauri.conf.json 中允許使用 API。

{
"tauri": {
"allowlist": {
"http": {
"all": true, // enable all http APIs
"request": true // enable HTTP request API
}
}
}
}

建議僅允許使用您會用到的 API,以最佳化套件大小和安全性。

安全性

此 API 具有範圍設定,強制您使用 glob 模式來限制可存取的 URL 和路徑。

例如,此範圍設定僅允許對 tauri-apps 組織的 GitHub API 提出 HTTP 要求。

{
"tauri": {
"allowlist": {
"http": {
"scope": ["https://api.github.com/repos/tauri-apps/*"]
}
}
}
}

嘗試執行 URL 未在範圍中設定的任何 API 會導致承諾遭到拒絕存取而被拒絕。

列舉

ResponseType

: 1.0.0

列舉成員

名稱類型定義於
3http.ts:74
1http.ts:72
2http.ts:73

類別

Body

POST 和 PUT 要求中要使用的主體物件。

: 1.0.0

屬性

payload

payload: unknown

定義於: http.ts:139

type

type: string

定義於: http.ts:138

方法

bytes

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

建立一個新的位元組陣列主體。

範例

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

參數

名稱類型說明
bytesIterable<number> | ArrayBuffer | ArrayLike<number>主體位元組陣列。

傳回: Body

準備好在 POST 和 PUT 要求中使用的主體物件。

form

靜態 form(data: FormInput): Body

建立一個新的表單資料主體。表單資料是一個物件,其中每個鍵是項目名稱,而值是一個字串或一個檔案物件。

預設會設定 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

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

建立新的 JSON 主體。

範例

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

參數

名稱類型說明
資料Record<any, any>主體 JSON 物件。

傳回: Body

準備好在 POST 和 PUT 要求中使用的主體物件。

text

Static text(value: string): Body

建立新的 UTF-8 字串主體。

範例

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

參數

名稱類型說明
value字串主體字串。

傳回: Body

準備好在 POST 和 PUT 要求中使用的主體物件。

Client

: 1.0.0

屬性

id

id: number

定義於: http.ts:316

方法

delete

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

參數

名稱類型
url字串
選項?RequestOptions

傳回:Promise<Response<T>>

drop

drop(): Promise<void>

中斷客戶端實例。

範例

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

傳回:Promise<void>

get

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

參數

名稱類型
url字串
選項?RequestOptions

傳回:Promise<Response<T>>

patch

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

參數

名稱類型
url字串
選項?RequestOptions

傳回:Promise<Response<T>>

post

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

參數

名稱類型
url字串
body?Body
選項?RequestOptions

傳回:Promise<Response<T>>

put

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

參數

名稱類型
url字串
body?Body
選項?RequestOptions

傳回:Promise<Response<T>>

request

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

參數

名稱類型
選項HttpOptions

傳回:Promise<Response<T>>

Response<T>

回應物件。

: 1.0.0

類型參數

  • T

屬性

data

data: T

回應資料。

定義於: http.ts:299

headers

headers: Record<string, string>

回應標頭。

定義於: http.ts:295

ok

ok: boolean

一個布林值,表示回應是否成功(狀態在 200–299 範圍內)或失敗。

定義於: http.ts:293

rawHeaders

rawHeaders: Record<string, string[]>

回應原始標頭。

定義於: http.ts:297

status

status: number

回應狀態碼。

定義於: http.ts:291

url

url: string

請求 URL。

定義於: http.ts:289

介面

ClientOptions

: 1.0.0

屬性

connectTimeout

Optional connectTimeout: number | Duration

定義於: http.ts:65

maxRedirections

Optional maxRedirections: number

定義客戶端應遵循的最大重新導向次數。如果設為 0,則不會遵循任何重新導向。

定義於: http.ts:64

Duration

: 1.0.0

屬性

nanos

nanos: number

定義於: http.ts:53

secs

secs: number

定義於: http.ts:52

FilePart<T>

: 1.0.0

類型參數

  • T

屬性

file

file: string | T

定義於: http.ts:81

fileName

Optional fileName: string

定義於: http.ts:83

mime

Optional mime: string

定義於: http.ts:82

HttpOptions

傳送到後端的選項物件。

: 1.0.0

屬性

body

Optional body: Body

定義於: http.ts:263

headers

Optional headers: Record<string, any>

定義於: http.ts:261

method

method: HttpVerb

定義於: http.ts:259

query

Optional query: Record<string, any>

定義於: http.ts:262

responseType

Optional responseType: ResponseType

定義於: http.ts:265

timeout

Optional timeout: number | Duration

定義於: http.ts:264

url

url: string

定義於: http.ts:260

類型別名

FetchOptions

FetchOptions: Omit<HttpOptions, "url">

fetch API 的選項。

定義於: http.ts:271

FormInput

FormInput: Record<string, Part> | FormData

定義於: http.ts:88

HttpVerb

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

請求 HTTP 動詞。

定義於: http.ts:242

Part

Part: string | Uint8Array | FilePart<Uint8Array>

定義於: http.ts:86

RequestOptions

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

請求選項。

定義於: http.ts:269

函式

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,
});

類型參數

  • T

參數

名稱類型
url字串
選項?FetchOptions

傳回:Promise<Response<T>>

getClient

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

使用指定的選項建立新的用戶端。

範例

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

: 1.0.0

參數

名稱類型說明
選項?ClientOptions用戶端設定。

傳回: Promise<Client>

解析為用戶端實例的承諾。