HTTP 用戶端
使用 http 外掛程式發出 HTTP 請求。
支援平台
此外掛程式需要 Rust 版本至少為 1.77.2
平台 | 層級 | 備註 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
設定
安裝 http 外掛程式以開始使用。
使用專案的套件管理器新增依賴項
npm run tauri add http
yarn run tauri add http
pnpm tauri add http
deno task tauri add http
bun tauri add http
cargo tauri add http
-
在
src-tauri
資料夾中執行以下命令,將外掛程式新增至Cargo.toml
中專案的依賴項cargo add tauri-plugin-http -
修改
lib.rs
以初始化外掛程式src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_http::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
如果您想在 JavaScript 中發出 http 請求,也請安裝 npm 套件
npm install @tauri-apps/plugin-httpyarn add @tauri-apps/plugin-httppnpm add @tauri-apps/plugin-httpdeno add npm:@tauri-apps/plugin-httpbun add @tauri-apps/plugin-http
用法
HTTP 外掛程式在 Rust 中以 reqwest 重新導出的形式提供,也在 JavaScript 中提供。
JavaScript
-
配置允許的 URL
src-tauri/capabilities/default.json {"permissions": [{"identifier": "http:default","allow": [{ "url": "https://*.tauri.app" }],"deny": [{ "url": "https://private.tauri.app" }]}]}如需更多資訊,請參閱 權限總覽 的文件
-
發送請求
fetch
方法盡可能接近且符合fetch
Web API。import { fetch } from '@tauri-apps/plugin-http';// Send a GET requestconst response = await fetch('http://test.tauri.app/data.json', {method: 'GET',});console.log(response.status); // e.g. 200console.log(response.statusText); // e.g. "OK"
Rust
在 Rust 中,您可以使用此外掛程式重新導出的 reqwest
crate。如需更多詳細資訊,請參閱 reqwest 文件。
use tauri_plugin_http::reqwest;
let res = reqwest::get("http://my.api.host/data.json").await;println!("{:?}", res.status()); // e.g. 200println!("{:?}", res.text().await); // e.g Ok("{ Content }")
預設權限
此權限集配置從 http 外掛程式可用的 fetch 操作類型。
這會啟用所有 fetch 操作,但不允許顯式提取任何來源。這需要在使用前手動配置。
已授與的權限
所有 fetch 操作均已啟用。
allow-fetch
allow-fetch-cancel
allow-fetch-read-body
allow-fetch-send
權限表
識別碼 | 描述 |
---|---|
|
啟用 fetch 命令,無需任何預先配置的範圍。 |
|
拒絕 fetch 命令,無需任何預先配置的範圍。 |
|
啟用 fetch_cancel 命令,無需任何預先配置的範圍。 |
|
拒絕 fetch_cancel 命令,無需任何預先配置的範圍。 |
|
啟用 fetch_read_body 命令,無需任何預先配置的範圍。 |
|
拒絕 fetch_read_body 命令,無需任何預先配置的範圍。 |
|
啟用 fetch_send 命令,無需任何預先配置的範圍。 |
|
拒絕 fetch_send 命令,無需任何預先配置的範圍。 |
© 2025 Tauri 貢獻者。CC-BY / MIT