运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

使用 fetch 发送 HTTP 请求

Bun 实现了 Web 标准的 fetch API,用于发送 HTTP 请求。要向某个 URL 发送 GET 请求:

fetch.ts
const response = await fetch("https://bun.sh");
const html = await response.text(); // HTML 字符串

要向 API 端点发送 POST 请求:

fetch.ts
const response = await fetch("https://bun.sh/api", {
  method: "POST",
  body: JSON.stringify({ message: "Hello from Bun!" }),
  headers: { "Content-Type": "application/json" },
});

const body = await response.json();