diff --git a/src/content/docs/features/http-client.mdx b/src/content/docs/features/http-client.mdx index 7beadd1b65..1947da9982 100644 --- a/src/content/docs/features/http-client.mdx +++ b/src/content/docs/features/http-client.mdx @@ -3,11 +3,125 @@ title: HTTP Client description: Access the HTTP client written in Rust. --- -import Stub from '@components/Stub.astro'; import PluginLinks from '@components/PluginLinks.astro'; +import { Tabs, TabItem } from '@astrojs/starlight/components'; +import CommandTabs from '@components/CommandTabs.astro'; - - Based on https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/http - +Make HTTP requests with the http plugin. + +## Setup + +Install the http plugin to get started. + + + + + 1. Use your project's package manager to add the dependency: + + + + 2. Modify `lib.rs` to initialize the plugin: + + {/* TODO: Revise once https://github.com/tauri-apps/tauri/issues/7696 is in */} + + ```rust ins={6} + // 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"); + } + ``` + + + + + 1. Run `cargo add tauri-plugin-http` to add the plugin to the project's dependencies in `Cargo.toml`. + + 2. Modify `lib.rs` to initialize the plugin: + + ```rust ins={6} + // 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"); + } + ``` + + 3. If you'd like to make http requests in JavaScript then install the npm package as well: + + + + + + + +## Usage + +The http plugin is available in both as an JavaScript API and in Rust as a [reqwest](https://docs.rs/reqwest/) re-export. + + +### JavaScript + +1. To keep your app safe, configure allowed scope. Read more on [JavaScript API reference](https://beta.tauri.app/2/reference/js/http/#security). + +```json +//tauri.conf.json +{ +"plugins": { + "http": { + "scope": ["http://my.api.host/*"] + } + } +} +``` +2. Send a request + +```js +import { fetch } from '@tauri-apps/plugin-http'; + +// Send a GET request +const response = await fetch("http://my.api.host/data.json", { + method: "GET", +}); +console.log(response.status); // e.g. 200 +console.log(response.statusText); // e.g. "OK" + +``` + +:::note + +The current `fetch` method is an API to Rust backend. It tries to be as close and compliant to the [`fetch` Web API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) as possible. + +::: + + +### Rust + + +In Rust you can utilize the `reqwest` crate re-exported by the plugin. For more details refer to [reqwest docs](https://docs.rs/reqwest/). + + +```rust +use tauri_plugin_http::reqwest; + +let res = reqwest::get("http://my.api.host/data.json").await; +println!("{:?}", res.status()); // e.g. 200 +println!("{:?}", res.text().await); // e.g Ok("{ Content }") +```