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
yarn run tauri add http
pnpm tauri add http
deno task tauri add http
bun tauri add http
cargo tauri add http
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-http -
Modify
lib.rs
to initialize the plugin: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");} -
If you’d like to make http requests in JavaScript then install the npm package as well:
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
使用
¥Usage
HTTP 插件在 Rust 中可用作 reqwest 重新导出和 JavaScript。
¥The HTTP plugin is available in both Rust as a reqwest re-export and JavaScript.
JavaScript
-
配置允许的 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
-
发送请求
¥Send a request
fetch
方法尝试尽可能接近并符合fetch
Web API。¥The
fetch
method tries to be as close and compliant to thefetch
Web API as possible.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":::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. 200println!("{:?}", 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 |
---|---|
|
Enables the fetch command without any pre-configured scope. |
|
Denies the fetch command without any pre-configured scope. |
|
Enables the fetch_cancel command without any pre-configured scope. |
|
Denies the fetch_cancel command without any pre-configured scope. |
|
Enables the fetch_read_body command without any pre-configured scope. |
|
Denies the fetch_read_body command without any pre-configured scope. |
|
Enables the fetch_send command without any pre-configured scope. |
|
Denies the fetch_send command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号