Bun

指南HTTP

使用 Bun 的 fetch 發送 HTTP 請求

Bun 實作 Web 標準 fetch API,用於發送 HTTP 請求。若要將簡單的 GET 請求傳送至 URL

const response = await fetch("https://bun.dev.org.tw");
const html = await response.text(); // HTML string

若要將 POST 請求傳送至 API 端點。

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

const body = await response.json();