HTTP Client(HTTP クライアント)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
「http プラグイン」を使用して HTTP リクエストを作成します。
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 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
「HTTP」プラグインは、JavaScrip と reqwest の再エクスポート(re-export)としての Rust の両方で利用可能です。
-
許可された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';// 「GET」リクエストの送信const response = await fetch('http://test.tauri.app/data.json', {method: 'GET',});console.log(response.status); // たとえば 200console.log(response.statusText); // たとえば "OK"
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 }")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. |
【※ この日本語版は、「Feb 22, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT