http

访问用Rust编写的HTTP客户端。

tauri.conf.json 中的 build.withGlobalTauri 设置为 true 时,此包也可以通过 window.__TAURI__.http 访问。

这些API必须在 tauri.conf.json 中列入允许列表:

{
  "tauri": {
    "allowlist": {
      "http": {
        "all": true, // enable all http APIs
        "request": true // enable HTTP request API
      }
    }
  }
}

建议仅列入您使用的API,以获得最佳的包大小和安全性。

Security

此API有一个作用域配置,强制您使用全局模式限制可访问的URL和路径。

例如,此作用域配置仅允许对tauri-apps组织的GitHub API进行HTTP请求:

{
  "tauri": {
    "allowlist": {
      "http": {
        "scope": ["https://api.github.com/repos/tauri-apps/*"]
      }
    }
  }
}

尝试执行任何未在作用域中配置的URL的API将导致因拒绝访问而承诺被拒绝。

Enumerations

ResponseType

Since: 1.0.0

枚举成员

名称

类型

定义在

Binary

3

http.ts:74

JSON

1

http.ts:72

Text

2

http.ts:73

Classes

Body

用于POST和PUT请求的主体对象。

Since: 1.0.0

属性

  • payload: unknown

定义在: http.ts:95

  • type: string

定义在: http.ts:94

方法

  • Static bytes(bytes: Iterable<number> | ArrayBuffer | ArrayLike<number>): Body

创建一个新的字节数组主体。

示例

import { Body } from "@tauri-apps/api/http"
Body.bytes(new Uint8Array([1, 2, 3]));

参数

名称

类型

描述

bytes

Iterable<number> | ArrayBuffer | ArrayLike<number>

请求体字节数组。

返回: Body

主体对象已准备好用于POST和PUT请求。

创建一个新的表单数据主体。表单数据是一个对象,其中每个键是条目名称,值是字符串或文件对象。

默认情况下,它会设置 application/x-www-form-urlencoded 内容类型头,但如果启用了Cargo功能 http-multipart,您可以将其设置为 multipart/form-data

注意,文件路径必须在 fs 允许列表范围内。

示例

import { Body } from "@tauri-apps/api/http"
const body = Body.form({
  key: 'value',
  image: {
    file: '/path/to/file', // either a path or an array buffer of the file contents
    mime: 'image/jpeg', // optional
    fileName: 'image.jpg' // optional
  }
});

// alternatively, use a FormData:
const form = new FormData();
form.append('key', 'value');
form.append('image', file, 'image.png');
const formBody = Body.form(form);

参数

名称

类型

描述

data

Record<string, Part>|FormData

请求体数据。

返回: Body

主体对象已准备好用于POST和PUT请求。

创建一个新的JSON请求体。

示例

import { Body } from "@tauri-apps/api/http"
Body.json({
  registered: true,
  name: 'tauri'
});

参数

名称

类型

描述

data

Record<any, any>

请求体JON对象。

主体对象已准备好用于POST和PUT请求。

  • Static text(value: string): Body

创建一个新的UTF-8字符串请求体。

示例

import { Body } from "@tauri-apps/api/http"
Body.text('The body content as a string');

参数

名称

类型

描述

value

string

请求体字符串。

返回: Body

主体对象已准备好用于POST和PUT请求。

Client

Since: 1.0.0

属性

  • id: number

定义在: http.ts:303

方法

发送DELETE请求。

示例

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.delete('http://localhost:3003/users/1');

类型参数

  • T

参数

名称

类型

url

string

options?

RequestOptions

返回: Promise<Response<T>>

销毁客户端实例。

示例

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
await client.drop();

返回: Promise<void>

发送GET请求。

示例

import { getClient, ResponseType } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.get('http://localhost:3003/users', {
  timeout: 30,
  // the expected response type
  responseType: ResponseType.JSON
});

类型参数

  • T

参数

名称

类型

url

string

options?

RequestOptions

返回: Promise<Response<T>>

发送PATCH请求。

示例

import { getClient, Body } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.patch('http://localhost:3003/users/1', {
  body: Body.json({ email: 'contact@tauri.app' })
});

类型参数

  • T

参数

名称

类型

url

string

options?

RequestOptions

返回: Promise<Response<T>>

发送POST请求。

示例

import { getClient, Body, ResponseType } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.post('http://localhost:3003/users', {
  body: Body.json({
    name: 'tauri',
    password: 'awesome'
  }),
  // in this case the server returns a simple string
  responseType: ResponseType.Text,
});

类型参数

  • T

参数

名称

类型

url

string

body?

Body

options?

RequestOptions

返回: Promise<Response<T>>

发送PUT请求。

示例

import { getClient, Body } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.put('http://localhost:3003/users/1', {
  body: Body.form({
    file: {
      file: '/home/tauri/avatar.png',
      mime: 'image/png',
      fileName: 'avatar.png'
    }
  })
});

类型参数

  • T

参数

名称

类型

url

string

body?

Body

options?

RequestOptions

返回: Promise<Response<T>>

发起一个HTTP请求。

示例

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.request({
  method: 'GET',
  url: 'http://localhost:3003/users',
});

类型参数

  • T

参数

名称

类型

options?

HttpOptions

返回: Promise<Response<T>>

Response<T>

响应对象。

Since: 1.0.0

类型参数

  • T

属性

  • data: T

响应数据

定义在: http.ts:286

  • headers: Record<string, string>

响应头。

定义在: http.ts:282

  • ok: boolean

一个布尔值,指示响应是否成功(状态在200-299范围内)。

定义在: http.ts:280

  • rawHeaders: Record<string, string[]>

响应的原始头信息。

定义在: http.ts:284

  • status: number

响应状态码。

定义在: http.ts:278

  • url: string

请求URL。

定义在: http.ts:276

Interfaces

ClientOptions

Since: 1.0.0

属性

  • Optional connectTimeout: number | Duration

定义在: http.ts:65

  • Optional maxRedirections: number

定义客户端应跟随的最大重定向次数。如果设置为0,则不会跟随任何重定向。

定义在: http.ts:64

Duration

Since: 1.0.0

属性

  • nanos: number

定义在: http.ts:53

  • secs: number

定义在: http.ts:52

FilePart<T>

Since: 1.0.0

类型参数

  • T

属性

  • file: string | T

定义在: http.ts:81

  • Optional fileName: string

定义在: http.ts:83

  • Optional mime: string

定义在: http.ts:82

HttpOptions

发送到后端的选项对象。

Since: 1.0.0

属性

  • Optional body: Body

定义在: http.ts:250

  • Optional headers: Record<string, any>

定义在: http.ts:248

定义在: http.ts:246

  • Optional query: Record<string, any>

定义在: http.ts:249

定义在: http.ts:252

定义在: http.ts:251

  • url: string

定义在: http.ts:247

Type Aliases

FetchOptions

fetch API的选项。

定义在: http.ts:258

HttpVerb

  • HttpVerb: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE"

请求的HTTP动词。

定义在: http.ts:229

Part

定义在: http.ts:86

RequestOptions

请求选项。

定义在: http.ts:256

Functions

fetch

使用默认客户端执行HTTP请求。

示例

import { fetch } from '@tauri-apps/api/http';
const response = await fetch('http://localhost:3003/users/2', {
  method: 'GET',
  timeout: 30,
});

类型参数

  • T

参数

名称

类型

url

string

options?

FetchOptions

返回: Promise<Response<T>>

getClient

使用指定的选项创建一个新的客户端。

示例

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();

Since: 1.0.0

参数

名称

类型

描述

options?

ClientOptions

客户端配置

返回: Promise<Client>

一个承诺对象,解析为客户端实例。