HTTP Client
Questi contenuti non sono ancora disponibili nella tua lingua.
Make HTTP requests with the http plugin.
Setup
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
bun tauri add http
cargo tauri add http
-
Run
cargo add tauri-plugin-http
to add the plugin to the project’s dependencies inCargo.toml
. -
Modify
lib.rs
to initialize the plugin: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");} -
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-httpbun add @tauri-apps/plugin-http
Usage
The http plugin is available in both as an JavaScript API and in Rust as a reqwest re-export.
JavaScript
-
Configure the allowed URLs
src-tauri/capabilities/base.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
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
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 }")
© 2024 Tauri Contributors. CC-BY / MIT