使用 fetch 傳送 HTTP 請求
Bun 實現了 Web 標準的 fetch API,用於傳送 HTTP 請求。要向某個 URL 傳送 GET 請求:
const response = await fetch("https://bun.sh");
const html = await response.text(); // HTML 字串
要向 API 端點發送 POST 請求:
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();