0Pricing
JavaScript Academy · Lesson

POST and Sending Data

Send JSON payloads with request options.

POST and Sending Data is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Sending Data to a Server

To create or update data you send a request with a body. Fetch supports this through the options object passed as the second argument.

Setting the Method

Set method to the HTTP verb you need: POST to create, PUT/PATCH to update, DELETE to remove.

fetch(url, { method: "POST" });

Adding a JSON Body

Most APIs expect JSON. Stringify your object with JSON.stringify and put it in body.

fetch(url, {
  method: "POST",
  body: JSON.stringify({ name: "Ada", age: 36 })
});

The Content-Type Header

Tell the server the body is JSON by setting Content-Type: application/json. Without it the server may misread the payload.

fetch(url, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Ada" })
});

Reading the Response

The server usually replies with the created resource. Read it the same way as a GET, with response.json().

const response = await fetch(url, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(payload)
});
const created = await response.json();

A Complete Create Function

Bundle the pattern into a reusable function. It accepts data, sends it, and returns the server response.

async function createUser(data) {
  const res = await fetch("/api/users", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(data)
  });
  return res.json();
}

PUT and PATCH

Use PUT to replace a resource entirely and PATCH to update only some fields. The body shape differs accordingly.

fetch("/api/users/7", {
  method: "PATCH",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ age: 37 })
});

Sending Form Data

For file uploads or form submissions use a FormData object as the body. Do NOT set Content-Type yourself; the browser adds the correct multipart boundary.

const form = new FormData();
form.append("avatar", fileInput.files[0]);
fetch("/upload", { method: "POST", body: form });

Authentication

Protected endpoints need credentials. Send a token in the Authorization header alongside Content-Type.

fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + token
  },
  body: JSON.stringify(data)
});

Cookies and Credentials

By default fetch does not send cookies cross-origin. Set credentials: "include" when the server uses cookie-based sessions.

fetch(url, { method: "POST", credentials: "include", body: payload });

Stringify Pitfalls

Remember: body must be a string (or FormData/Blob). Passing a raw object sends [object Object]. Always JSON.stringify objects first.

body: JSON.stringify({ ok: true }) // correct
// body: { ok: true }  <-- broken

Quick Check

Sending JSON with POST.

Recap

Send data by setting method, a body, and headers. For JSON, JSON.stringify the object and set Content-Type: application/json. Use FormData for uploads (no manual Content-Type), add Authorization for auth, and credentials: "include" for cookies. Read the reply with response.json().

Frequently asked questions

Is the “POST and Sending Data” lesson free?

Yes — the full text of “POST and Sending Data” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “POST and Sending Data”?

Send JSON payloads with request options. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “POST and Sending Data” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Making GET Requests
  2. POST and Sending Data
  3. Handling Errors and Status Codes
  4. Aborting Requests with AbortController
← Back to JavaScript Academy