跳到主要内容

HTTP 客户端

🧩 插件概述:@tauri-apps/plugin-http 用于在 Tauri 应用中触发 HTTP 请求,可以在 JS/TS 侧和 Rust 侧使用。

✅ 支持平台

  • ✅ Windows / macOS / Linux
  • ✅ iOS / Android(移动端)
  • 需要 Rust ≥ 1.77.2

📦 安装方式

自动安装(推荐)

pnpm run tauri add http

或手动安装

JavaScript/TypeScript 项目

pnpm add @tauri-apps/plugin-http

Rust 依赖(在 src-tauri/Cargo.toml 中)

[dependencies]
tauri-plugin-http = "2"

📁 权限配置(关键)

// src-tauri/capabilities/default.json
{
"permissions": [
{
"identifier": "http:default",
"allow": [
{ "url": "https://*.tauri.app" },
{ "url": "https://your.api.com" },
{ "url": "http://localhost:3000" }
],
"deny": [{ "url": "https://private.tauri.app" }]
}
]
}

"http:allow-fetch",
"http:allow-fetch-cancel",
"http:allow-fetch-read-body",
"http:allow-fetch-send",
{
"identifier": "http:default",
"allow": [
{
"url": "http://**"
},
{
"url": "https://**"
},
{
"url": "http://*:*"
},
{
"url": "https://*:*"
}
]
},

🚀 JS 中使用示例

import { fetch } from "@tauri-apps/plugin-http";

const response = await fetch("https://your.api.com/data", {
method: "GET",
});
console.log(response.status); // 200
console.log(await response.text()); // 返回内容

⚠️ 注意事项

❗ 默认会忽略某些“危险”请求头(如 Authorization)

如果你需要设置 Authorization、User-Agent 等“受保护”的 header,需开启:

Cargo.toml 中启用 unsafe-headers 功能:

[dependencies]
tauri-plugin-http = { version = "2", features = ["unsafe-headers"] }

🦀 Rust 侧用法(使用 reqwest)

Tauri 插件自动 re-export reqwest,可直接使用:

use tauri_plugin_http::reqwest;

#[tauri::command]
async fn my_fetch() -> Result<String, String> {
let res = reqwest::get("https://my.api.com/data.json")
.await
.map_err(|e| e.to_string())?;

let text = res.text().await.map_err(|e| e.to_string())?;
Ok(text)
}

🔐 权限标识参考表(默认全部启用)

Identifier含义
http:allow-fetch允许 fetch 请求
http:allow-fetch-cancel支持取消请求
http:allow-fetch-read-body支持读取响应体
http:allow-fetch-send支持发送带 body 的请求

你可以选择精细控制某些权限是否启用。


✅ 小结:常见问题快速定位

问题原因与解决方案
url not allowed on the configured scope没有在 default.json 中配置允许的 URL
fetch not allowed. Permissions...没有启用 http:allow-fetch 等权限
Authorization header 无效需要在 Cargo.toml 启用 unsafe-headers
POST 请求失败(body 被忽略)需要开启 allow-fetch-send 权限

如果你提供你实际请求的 URL 和配置,我可以帮你检查是否缺了哪些权限或 URL 配置。是否需要我帮你生成一个完整可用的模板配置?

在 Tauri 应用中,你可以选择两种方式发起 HTTP 请求:

  1. tauri-plugin-http 插件方式
  2. Web 端原生 fetch(浏览器 API)方式

这两者的区别、优势与劣势如下:


🔌 1. tauri-plugin-http

✅ 优势

说明
绕过 CORS 限制作为 Native 层实现的 HTTP 请求,不受浏览器 CORS 限制,可以请求任意接口。适合访问私有 API 或非 CORS 配置的服务。
支持文件下载/上传等底层特性插件支持设置 header、method、body 类型、上传表单、多 part、stream 等,比 fetch 更接近 Node.js 或 curl 的行为。
更安全/可控Tauri 提供作用域控制,可设置白名单,防止任意 URL 被访问(通过 tauri.conf.json > allowlist.http)。
可跨域访问内网地址即使是 .local.svc 之类的接口,也能请求(如企业内网 API)。
可更方便进行 SSL pinning比如你做 SSL 证书绑定(certificate pinning)场景时,用 Rust 原生库更适合。

❌ 劣势

说明
引入 Rust 插件/需配置权限必须在 src-tauri 中引入并注册插件,还要配置权限和作用域。
调试不如 Web fetch 简单插件请求出错时定位要查 Tauri Console + Rust Error,比 DevTools 要繁琐。
体积更大会引入额外 Rust crate,如 reqwest,增加二进制大小。

🌐 2. 浏览器原生 fetch

✅ 优势

说明
原生支持,不需额外插件开箱即用,直接写 fetch 代码,无需 tauri 插件或 Rust 编译。
调试方便在 DevTools 里查看请求、响应、header、network timeline 都非常方便。
易于前后端统一代码逻辑如果你同时部署 Web 版本与 Tauri 桌面端,使用 fetch 能复用同一套代码(比如用 axios/fetch 封装 API 请求)。

❌ 劣势

说明
受 CORS 限制如果目标服务器未设置 Access-Control-Allow-Origin,请求会失败。
不能访问非浏览器信任域如内网域名、没有 HTTPS 的地址、非 80/443 端口等,有时会被拦截。
不适合做高权限/隐私请求比如 SSL pinning、token 注入、绕过代理等情况,fetch 无法满足。

📊 总结对比表

特性tauri-plugin-httpfetch
是否受 CORS 限制❌ 否✅ 是
是否可访问内网地址✅ 是❌ 否
请求权限可控✅ 强(白名单)❌ 弱
调试体验❌ 一般✅ 好
是否需引入插件✅ 需要❌ 不需要
是否适合统一 Web/桌面逻辑❌ 一般✅ 适合
支持上传文件/流等复杂请求✅ 强❌ 限制较多
支持 SSL Pinning✅ 可实现❌ 不支持
请求性能✅ 原生快✅ 浏览器快
支持跨平台(Win/macOS/Linux)✅ 是✅ 是,但受浏览器引擎限制

🧠 建议使用场景

  • ✅ 使用 tauri-plugin-http

    • 需要绕过 CORS
    • 内网接口或 .local.svc 域名请求
    • 安全/敏感接口调用(比如 token 登录、登录设备认证)
    • 文件流上传下载(特别是大文件)
    • 需要控制访问白名单、做权限管理
  • ✅ 使用 fetch

    • 公网 API,支持 CORS
    • 与 Web 项目共享前端逻辑
    • 调试为主、开发早期阶段
    • 简单的 GET/POST 接口调用

如你在开发桌面应用中访问公司私有化部署服务(如 .svc.matrx.tech),推荐用 tauri-plugin-http 并设置 allowlist

如果你还有具体代码片段或使用场景,我可以帮你选择哪种方式更合适。