fetch POST
Create a resource: method POST, JSON body, Content-Type header.
POST usually sends a body. Build a JS object, turn it into a string with
JSON.stringify (Phase 3), set
Content-Type: application/json, and pass
method: "POST". JSONPlaceholder echoes a fake created post.
Server echo appears here.
Important JavaScript
const payload = { title: "Scout", body: "Notes", userId: 1 };
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
}
);
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
const created = await response.json();