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.
使用项目的包管理器添加依赖:
npm run tauri add httpyarn run tauri add httppnpm tauri add httpdeno task tauri add httpbun tauri add httpcargo 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
🌐 Usage
HTTP 插件在 Rust 中作为 reqwest 的重新导出以及在 JavaScript 中可用。
🌐 The HTTP plugin is available in both Rust as a reqwest re-export and JavaScript.
-
配置允许的 URL
src-tauri/capabilities/default.json {"permissions": [{"identifier": "http:default","allow": [{ "url": "https://*.tauri.app" }],"deny": [{ "url": "https://private.tauri.app" }]}]}欲了解更多信息,请参阅权限概览的文档
-
发送请求
fetch方法尽可能接近并遵循fetchWeb 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 中,你可以使用插件重新导出的 reqwest crate。更多详情请参考 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.
This default permission set includes the following:
allow-fetchallow-fetch-cancelallow-fetch-sendallow-fetch-read-bodyallow-fetch-cancel-body
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_cancel_body command without any pre-configured scope. |
|
|
Denies the fetch_cancel_body 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号
Nodejs.cn 旗下网站