跳到主要內容
Tauri

@tauri-apps/plugin-http

使用 Rust 後端發出 HTTP 請求。

安全性

此 API 具有作用域設定,強制您使用 glob 模式限制可存取的 URL。

例如,此作用域設定僅允許對 tauri.app 的所有子網域發出 HTTP 請求,但不包括 https://private.tauri.app

{
"permissions": [
{
"identifier": "http:default",
"allow": [{ "url": "https://*.tauri.app" }],
"deny": [{ "url": "https://private.tauri.app" }]
}
]
}

嘗試使用作用域中未設定的 URL 執行任何 API 都會導致承諾遭到拒絕,因為存取被拒絕。

介面

ClientOptions

用於設定 Rust 客戶端以發出 fetch 請求的選項

Since

2.0.0

屬性

屬性類型描述定義於
connectTimeout?number逾時時間(毫秒)來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L82
danger?DangerousSettings用於設定客戶端危險設定的配置,例如停用 SSL 驗證。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L90
maxRedirections?number定義客戶端應追蹤的最大重新導向次數。若設定為 0,則不會追蹤任何重新導向。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L80
proxy?ProxyClient 應將請求傳遞到的代理伺服器配置。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L86

DangerousSettings

用於設定客戶端危險設定的配置,例如停用 SSL 驗證。

Since

2.3.0

屬性

屬性類型描述定義於
acceptInvalidCerts?boolean停用 SSL 驗證。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L102
acceptInvalidHostnames?boolean停用主機名稱驗證。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L106

Proxy

Client 應將請求傳遞到的代理伺服器配置。

Since

2.0.0

屬性

屬性類型描述定義於
all?string | ProxyConfig將所有流量代理到傳遞的 URL。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L40
http?string | ProxyConfig將所有 HTTP 流量代理到傳遞的 URL。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L44
https?string | ProxyConfig將所有 HTTPS 流量代理到傳遞的 URL。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L48

ProxyConfig

屬性

屬性類型描述定義於
basicAuth?object使用 Basic auth 設定 Proxy-Authorization 標頭。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L59
basicAuth.passwordstring-來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L61
basicAuth.usernamestring-來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L60
noProxy?string用於篩選出不應被代理的請求的配置。條目應以逗號分隔(條目之間的空白會被忽略)來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L67
urlstring代理伺服器的 URL。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L55

函式

fetch()

function fetch(input, init?): Promise<Response>

從網路擷取資源。它回傳一個 Promise,該 Promise 會解析為該 RequestResponse,無論是否成功。

參數

參數類型
inputstring | URL | Request
init?RequestInit & ClientOptions

回傳

Promise<Response>

範例

const response = await fetch("http://my.json.host/data.json");
console.log(response.status); // e.g. 200
console.log(response.statusText); // e.g. "OK"
const jsonData = await response.json();

Since

2.0.0

來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/http/guest-js/index.ts#L125


© 2025 Tauri Contributors. CC-BY / MIT