今天來簡單的介紹一下Tauri裡的http,http能讓我們訪問用Rust編寫的HTTP Client端。
如果我們要使用要使用window.__TAURI__.http來獲取的話,要到todo\src-tauri\tauri.conf.json將其設定為true
ex.tauri.conf.json
{
  "tauri": {
    "allowlist": {
      "http": {
        "all": true, // enable all http APIs
        "request": true // enable HTTP request API
      }
    }
  }
}
我們也可以對我們的URL和路徑來制定scope,來達到只允許向此Scope發出HTTP請求
ex.
{
  "tauri": {
    "allowlist": {
      "http": {
        "scope": ["https://api.github.com/repos/tauri-apps/*"]
      }
    }
  }
}
Body :用於 POST 和 PUT 請求的主體對象Client :關於定義Client端的對象Response<T> :關於響應對象fetch<T>(url: string, options?: FetchOptions): Promise<Response<T>> : 使用默認Client端執行 HTTP 請求
getClient(options?: ClientOptions): Promise<Client> : 使用指定的選項創建一個新Client端
ex.透過fetch來獲取資料並傳遞到我們fronted端
import { fetch } from '@tauri-apps/api/http';
const response = await fetch('http://localhost:3003/users/1', {
  method: 'GET',
  timeout: 30,
});
fetch和invoke的差別,在於fetch是我們frontend從網路上來獲取資料,而invoke則是從frontend和backend間來傳遞資料。為什麼Tauri不直接從backend端直接從網路上來獲取資料,因為這樣子能增強安全性,藉由frontend端來做隔離,避免惡意資料直接傳遞到backend端,並在系統上直接進行操作。
今天簡單的介紹了Tauri的http方式,明天我們接著將我們的App來獲取目前的天氣,我們明天見