Bun 实现了 Web 标准的 fetch API 用于发送 HTTP 请求。要向 URL 发送简单的 GET 请求:
ts
const response = await fetch("https://bun.com");
const html = await response.text(); // HTML 字符串要向 API 端点发送 POST 请求:
ts
const response = await fetch("https://bun.com/api", {
method: "POST",
body: JSON.stringify({ message: "Hello from Bun!" }),
headers: { "Content-Type": "application/json" },
});
const body = await response.json();