Send JSON with fetch POST
Send a JSON body with fetch POST by stringifying the object and setting the Content-Type header, since fetch does not do either of those for you.
Passing an object as body sends the text [object Object]. The body must be a string, and the server needs the content type to parse it.
const res = await fetch('https://httpbin.org/post', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Ada', role: 'admin' }),
});
const result = await res.json();
console.log(result.json);
console.log(result.headers['Content-Type']);{ name: 'Ada', role: 'admin' }
application/jsonJSON.stringify turns the object into a string. Without the Content-Type: application/json header, the request is sent as text/plain and most frameworks will not parse the body. The httpbin.org/post endpoint echoes what it received, so you can see both. See Supplying request options.
Values such as undefined, functions, Date objects and BigInt need care: dates become ISO strings and BigInt throws. A cross-origin POST with this header triggers a CORS preflight request.