跳到内容
Tauri

HTTP 客户端

使用 http 插件发起 HTTP 请求。

此插件需要 Rust 版本至少为 **1.77.2**

平台 级别 备注
windows
linux
macos
android
ios

安装 http 插件以开始使用。

使用你的项目包管理器添加依赖项

npm run tauri add http

HTTP 插件既可以在 Rust 中作为 reqwest 的重新导出使用,也可以在 JavaScript 中使用。

  1. 配置允许的 URL

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

    欲了解更多信息,请参阅 权限概述 文档

  2. 发送请求

    fetch 方法旨在尽可能兼容 fetch Web API

    import { fetch } from '@tauri-apps/plugin-http';
    // Send a GET request
    const response = await fetch('http://test.tauri.app/data.json', {
    method: 'GET',
    });
    console.log(response.status); // e.g. 200
    console.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. 200
println!("{:?}", res.text().await); // e.g Ok("{ Content }")

默认权限

此权限集配置了 http 插件可用的 fetch 操作类型。

这启用了所有 fetch 操作,但不允许明确获取任何源。使用前需要手动配置。

已授予权限

启用了所有 fetch 操作。

此默认权限集包括以下内容

  • allow-fetch
  • allow-fetch-cancel
  • allow-fetch-send
  • allow-fetch-read-body
  • allow-fetch-cancel-body

权限表

标识符 描述

http:allow-fetch

启用不带任何预配置作用域的 fetch 命令。

http:deny-fetch

禁用不带任何预配置作用域的 fetch 命令。

http:allow-fetch-cancel

启用不带任何预配置作用域的 fetch_cancel 命令。

http:deny-fetch-cancel

禁用不带任何预配置作用域的 fetch_cancel 命令。

http:allow-fetch-cancel-body

启用不带任何预配置作用域的 fetch_cancel_body 命令。

http:deny-fetch-cancel-body

禁用不带任何预配置作用域的 fetch_cancel_body 命令。

http:allow-fetch-read-body

启用不带任何预配置作用域的 fetch_read_body 命令。

http:deny-fetch-read-body

禁用不带任何预配置作用域的 fetch_read_body 命令。

http:allow-fetch-send

启用不带任何预配置作用域的 fetch_send 命令。

http:deny-fetch-send

禁用不带任何预配置作用域的 fetch_send 命令。


© 2026 Tauri 贡献者。CC-BY / MIT