執行環境套件管理器打包器測試執行器程式碼檢查格式化指南參考部落格意見回饋

使用 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();