---
url: /reference/functions/network/fetch.md
---

# fetch

`fetch` 模块内置方法用于向外部资源发起 HTTP 请求，适用于访问第三方服务（如 GitHub、Slack、自建服务等）。

导入：

```typescript
import { fetch } from "@pc-nexus/network";
```

内置方法：

## request

`request` 函数用于向外部资源发起 HTTP 请求。

### 函数签名

```typescript
function request(url: string, options?: RequestInit): Promise<Response>
```

### 参数

`RequestInit` 类型部分属性定义如下：

|名称|描述|
|---|---|
|`method`|HTTP 请求方法|
|`headers`|自定义请求头。 - 支持标准的 HTTP 请求头字段，如 `Content-Type` 、 `Accept` 、 `Authorization` 等 - 以 `x-nexus-` 开头的请求头为 Nexus 系统内部使用，用户不可定义使用，如果用户尝试设置此类请求，系统将自动覆盖其值。|
|`body`|请求体内容|

要了解完整的实现细节，请参阅 `undici` 文档中的在 [Node.js 中使用 Fetch API 与 Undici](https://nodejs.org/learn/getting-started/fetch#using-the-fetch-api-with-undici-in-nodejs) 这一章节。

### 返回值

返回标准的 `Response` 对象，可通过 `response.status` 、 `response.ok` 、 `response.json()` 、 `response.text()` 等标准方法处理响应。

### 权限声明

使用 `fetch` 访问外部资源前，需要在 `manifest.yaml` 中通过声明允许访问的域名。未声明的域名请求将被运行时拦截，同时抛出错误。

```yaml
permissions:
  scopes: []
  external:
    fetch:
      backend:
        - "api.example.com"        # 允许访问的域名
        - "*.example-dev.com"       # 支持通配符匹配子域名
```

关于权限声明详情请参考： [External permissions](/reference/manifest/permissions-external)

### 示例

#### GET 请求

```typescript
import { fetch } from "@pc-nexus/network";

const response = await fetch.request("https://api.example.com/users", {
    method: "GET",
    headers: {
        "Accept": "application/json",
    },
});

const data = await response.json();
console.log(data);
```

#### POST 请求

```typescript
import { fetch } from "@pc-nexus/network";

const response = await fetch.request("https://api.example.com/users", {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
    body: {
        name: "张三",
        email: "zhangsan@example.com",
    },
});

const result = await response.json();
```
