Skip to content

Commit

Permalink
feat: Create plugin http guide (#1725)
Browse files Browse the repository at this point in the history
* first draft

* Update http-client.mdx

* // lib.rs

* Update http-client.mdx

* update rust example

* use ins={6}

* Update src/content/docs/features/http-client.mdx

Co-authored-by: Lorenzo Lewis <[email protected]>

* use normal text instead of headings

* add link to js api reference

* Add fetch web API link

---------

Co-authored-by: Lorenzo Lewis <[email protected]>
  • Loading branch information
vasfvitor and lorenzolewis authored Dec 19, 2023
1 parent 0a4ffc3 commit 348e0f1
Showing 1 changed file with 118 additions and 4 deletions.
122 changes: 118 additions & 4 deletions src/content/docs/features/http-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

<PluginLinks plugin="http" />

<Stub>
Based on https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/http
</Stub>
Make HTTP requests with the http plugin.

## Setup

Install the http plugin to get started.

<Tabs>
<TabItem label="Automatic">

1. Use your project's package manager to add the dependency:

<CommandTabs npm="npm run tauri add http"
yarn="yarn run tauri add http"
pnpm="pnpm tauri add http"
cargo="cargo tauri add http" />

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");
}
```

</TabItem>
<TabItem label="Manual">

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:

<CommandTabs
npm="npm install @tauri-apps/plugin-http"
yarn="yarn add @tauri-apps/plugin-http"
pnpm="pnpm add @tauri-apps/plugin-http"
/>

</TabItem>

</Tabs>

## 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 }")
```

0 comments on commit 348e0f1

Please sign in to comment.