Skip to content
Tauri 中文网

HTTP 客户端

使用 http 插件发出 HTTP 请求。

¥Make HTTP requests with the http plugin.

支持的平台

¥Supported Platforms

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

设置

¥Setup

安装 http 插件即可开始使用。

¥Install the http plugin to get started.

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

¥Use your project’s package manager to add the dependency:

npm run tauri add http

使用

¥Usage

HTTP 插件在 Rust 中可用作 reqwest 重新导出和 JavaScript。

¥The HTTP plugin is available in both Rust as a reqwest re-export and JavaScript.

JavaScript

  1. 配置允许的 URL

    ¥Configure the allowed URLs

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

    有关更多信息,请参阅 权限概述 的文档

    ¥For more information, please see the documentation for Permissions Overview

  2. 发送请求

    ¥Send a request

    fetch 方法尝试尽可能接近并符合 fetch Web API

    ¥The fetch method tries to be as close and compliant to the fetch Web API as possible.

    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"

    :::note 注意

    禁止的请求标头 默认被忽略。要使用它们,你必须启用 unsafe-headers 功能标志:

    ¥Forbidden request headers are ignored by default. To use them you must enable the unsafe-headers feature flag:

    src-tauri/Cargo.toml
    [dependencies]
    tauri-plugin-http = { version = "2", features = ["unsafe-headers"] }

    :::

Rust

在 Rust 中,你可以利用插件重新导出的 reqwest 包。有关更多详细信息,请参阅 reqwest 文档

¥In Rust you can utilize the reqwest crate re-exported by the plugin. For more details refer to reqwest docs.

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 }")

Default Permission

This permission set configures what kind of fetch operations are available from the http plugin.

This enables all fetch operations but does not allow explicitly any origins to be fetched. This needs to be manually configured before usage.

Granted Permissions

All fetch operations are enabled.

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

Permission Table

Identifier Description

http:allow-fetch

Enables the fetch command without any pre-configured scope.

http:deny-fetch

Denies the fetch command without any pre-configured scope.

http:allow-fetch-cancel

Enables the fetch_cancel command without any pre-configured scope.

http:deny-fetch-cancel

Denies the fetch_cancel command without any pre-configured scope.

http:allow-fetch-read-body

Enables the fetch_read_body command without any pre-configured scope.

http:deny-fetch-read-body

Denies the fetch_read_body command without any pre-configured scope.

http:allow-fetch-send

Enables the fetch_send command without any pre-configured scope.

http:deny-fetch-send

Denies the fetch_send command without any pre-configured scope.


Tauri 中文网 - 粤ICP备13048890号