Fetch POST with JSON Body

Send a POST request with a JSON payload.

Code

JavaScript
const response = await fetch("https://api.example.com/users", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Alice", age: 30 })
});

const data = await response.json();
console.log(data);

Line-by-line explanation

Expected output

{ id: 1, name: 'Alice' }

Related snippets

Related DuskTools