Course contentsShow
← JavaScript
Lesson 2269 of 2,72347. Networking PrimitivesFree lesson

Request Headers Object

Explore the Headers interface and how to set, get, and manipulate headers on a Request object.

Request Headers Object

What you'll learn: How to use the Headers interface to work with HTTP headers on your Request objects.


When making network requests, headers are the metadata that travels alongside your actual data—think of them like the envelope information on a letter (sender, priority, special instructions). The Headers interface gives you a structured, object-like way to manipulate these key-value pairs.

Creating a Headers Object

You can create a Headers instance in several ways:

// Empty headers object
const headers = new Headers();

// From an object literal
const headers2 = new Headers({
  'Content-Type': 'application/json',
  'Authorization': 'Bearer abc123'
});

// From an array of key-value pairs
const headers3 = new Headers([
  ['Content-Type', 'application/json'],
  ['X-Custom-Header', 'myvalue']
]);

Manipulating Headers

The Headers interface provides methods to get, set, check, and delete headers:

const headers = new Headers();

// Add or update headers
headers.set('Content-Type', 'application/json');
headers.append('Accept', 'application/json'); // similar to set, but can add multiple values

// Read headers
console.log(headers.get('Content-Type')); // 'application/json'
console.log(headers.has('Authorization')); // false

// Remove headers
headers.delete('Content-Type');

Using Headers with Request

Pass your Headers object to a Request:

const headers = new Headers({
  'Authorization': 'Bearer token123'
});

const request = new Request('https://api.example.com/data', {
  method: 'POST',
  headers: headers
});

await fetch(request);

Key Takeaway: The Headers interface provides a clean, method-based API for setting, getting, checking, and deleting HTTP headers on Request objects, giving you fine-grained control over request metadata.