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
包。有关详细信息,请参阅 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贡献者。署名-相同方式共享/MIT许可