Bun

指南HTTP

使用 Bun 发送 HTTP 请求

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

const response = await fetch("https://bun.net.cn");
const html = await response.text(); // HTML string

要向 API 端点发送一个 POST 请求。

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

const body = await response.json();