HTTP 客户端
使用 HTTP 插件发起 HTTP 请求。
Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
设置
请安装 http 插件。
使用项目的包管理器来添加依赖:
npm run tauri add http
yarn run tauri add http
pnpm tauri add http
bun tauri add http
cargo tauri add http
-
运行
cargo add tauri-plugin-http
命令,将插件添加到项目的cargo.toml
依赖中。 -
修改 lib.rs 来初始化插件。
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() // Initialize the plugin .plugin(tauri_plugin_http::init()) .run(tauri::generate_context!()) .expect("error while running tauri application");}
- 如果你想用 JavaScript 发送 http 请求,还需要安装 npm 包。
npm install @tauri-apps/plugin-http
yarn add @tauri-apps/plugin-http
pnpm add @tauri-apps/plugin-http
bun add @tauri-apps/plugin-http
用法
http 插件既有 JavaScript API 版本,也有 Rust reqwest 重新导出的版本。
JavaScript
-
配置允许访问的 URL
src-tauri/capabilities/base.json {"permissions": [{"identifier": "http:default","allow": [{ "url": "https://*.tauri.app" }],"deny": [{ "url": "https://private.tauri.app" }]}]}更多信息,请参阅权限概述的文档。
-
发送请求
import { fetch } from '@tauri-apps/plugin-http';// Send a GET requestconst response = await fetch('http://my.api.host/data.json', {method: 'GET',});console.log(response.status); // e.g. 200console.log(response.statusText); // e.g. "OK"
Rust
在 Rust 中,你可以利用插件重新导出的 reqwest
包。更多细节请参考 reqwest 文档。
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 }")
© 2025 Tauri Contributors. CC-BY / MIT