> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Bun 中通过 unix 域套接字进行 fetch

在 Bun 中，`fetch()` 的 `unix` 选项允许你通过 [unix 域套接字](https://en.wikipedia.org/wiki/Unix_domain_socket) 发送 HTTP 请求。

```ts fetch-unix.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
const unix = "/var/run/docker.sock";

const response = await fetch("http://localhost/info", { unix });

const body = await response.json();
console.log(body); // { ... }
```

***

`unix` 选项是一个字符串，指定了 unix 域套接字的本地文件路径。`fetch()` 函数会使用该套接字将请求发送到服务器，而不是使用 TCP 网络连接。通过在 URL 中使用 `https://` 协议代替 `http://`，也支持 `https`。

要通过 unix 域套接字发送 `POST` 请求到 API 端点：

```ts fetch-unix.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
const response = await fetch("https://hostname/a/path", {
  unix: "/var/run/path/to/unix.sock",
  method: "POST",
  body: JSON.stringify({ message: "Hello from Bun!" }),
  headers: {
    "Content-Type": "application/json",
  },
});

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