JavaScript POST JSON

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

To POST JSON, set method: "POST", Content-Type: application/json, and body: JSON.stringify(data).

JavaScript Syntax

JavaScript
const init = {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Ada" })
};
console.log(init.method);
console.log(JSON.parse(init.body).name);
Try it Editor

How It Works

  1. method. POST.
  2. headers. Declare JSON.
  3. body. Stringified payload.

Examples

Example 1 — Round-trip body

What the server receives.

JavaScript
const body = JSON.stringify({ ok: true });
console.log(JSON.parse(body).ok);
Try it Editor

How It Works

  1. Always stringify objects for JSON bodies.

Example 2 — Handle response

Same ok check.

JavaScript
async function send() {
  const res = { ok: true, status: 201, async json() { return { id: 3 }; } };
  if (!res.ok) throw new Error("HTTP " + res.status);
  console.log((await res.json()).id);
}
send();
Try it Editor

How It Works

  1. Created responses are still ok when 2xx.

Frequently Asked Questions

Yes for JSON APIs. fetch will not stringify objects for you.
For multipart forms, use FormData and let the browser set the Content-Type.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful