Add headers with new headers js

Here is an example of how to add headers using the headers library in JavaScript:

Method 1: Adding a single header

const headers = new Headers();
headers.append('Content-Type', 'application/json');

Method 2: Adding multiple headers

const headers = new Headers({
  'Content-Type': 'application/json',
  'Authorization': 'Bearer YOUR_API_KEY',
  'Accept': 'application/json'
});

Method 3: Adding headers to a request

fetch('https://example.com/api/data', {
  method: 'GET',
  headers: new Headers({
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY'
  })
});

In the above examples, we create a new Headers object and then use the append() method to add a single header. Alternatively, we can pass an object with key-value pairs to the Headers constructor to add multiple headers at once.

We can also use the set() method to set a header, it will overwrite any existing header with the same name.

headers.set('Content-Type', 'text/plain');

You can also use the delete() method to delete a header

headers.delete('Authorization');

You can also use the get() method to get the value of a header

headers.get('Content-Type'); // returns 'application/json'

Note that the Headers object is immutable, so you can't modify it after it's created. If you need to modify the headers, you'll need to create a new Headers object with the updated headers.

Also, you can use the headers object with other libraries such as Axios, jQuery, etc.