HTTP 客户端
使用 http 插件发起 HTTP 请求。
此插件需要 Rust 版本至少为 **1.77.2**
| 平台 | 级别 | 备注 |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
安装 http 插件以开始使用。
使用你的项目包管理器添加依赖项
npm run tauri add httpyarn run tauri add httppnpm tauri add httpdeno task tauri add httpbun tauri add httpcargo 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 中使用。
-
配置允许的 URL
src-tauri/capabilities/default.json {"permissions": [{"identifier": "http:default","allow": [{ "url": "https://*.tauri.app" }],"deny": [{ "url": "https://private.tauri.app" }]}]}欲了解更多信息,请参阅 权限概述 文档
-
发送请求
fetch方法旨在尽可能兼容fetchWeb 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 中,你可以使用插件重新导出的 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-fetchallow-fetch-cancelallow-fetch-sendallow-fetch-read-bodyallow-fetch-cancel-body
权限表
| 标识符 | 描述 |
|---|---|
|
|
启用不带任何预配置作用域的 fetch 命令。 |
|
|
禁用不带任何预配置作用域的 fetch 命令。 |
|
|
启用不带任何预配置作用域的 fetch_cancel 命令。 |
|
|
禁用不带任何预配置作用域的 fetch_cancel 命令。 |
|
|
启用不带任何预配置作用域的 fetch_cancel_body 命令。 |
|
|
禁用不带任何预配置作用域的 fetch_cancel_body 命令。 |
|
|
启用不带任何预配置作用域的 fetch_read_body 命令。 |
|
|
禁用不带任何预配置作用域的 fetch_read_body 命令。 |
|
|
启用不带任何预配置作用域的 fetch_send 命令。 |
|
|
禁用不带任何预配置作用域的 fetch_send 命令。 |
© 2026 Tauri 贡献者。CC-BY / MIT