POST with Fetch
Pass an options object with method, headers, and body to send data. Always set Content-Type when sending JSON.
Pass an options object with method, headers, and body to send data. Always set Content-Type when sending JSON.
const res = await fetch("/api/users", {
method: "POST",
headers: { "Content-Type": "application/json", "Accept": "application/json" },
body: JSON.stringify({ name: "Alice", email: "alice@example.com" }),
});
const created = await res.json();
console.log(created.id);
JSON.stringify() the body and set Content-Type: application/json — fetch does not do this automatically.
More in JavaScript