Categories
JavaScript Answers

How to set the content-type of request header when using Fetch API with JavaScript?

Spread the love

To set the content-type of request header when using Fetch API with JavaScript, we set the headers property.

For instance, we write

const options = {
  method: method,
  headers: new Headers({ "content-type": "application/json" }),
  mode: "no-cors",
};

options.body = JSON.stringify(body);
const res = await fetch(url, options);

to set the headers property to a Headers object with the content-type request header.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *