Response Headers
You'll learn how to read and interpret the metadata that the server sends back with every HTTP response.
What Are Response Headers?
When a server replies to your fetch() request, it doesn't just send the data you asked for—it also includes headers: key-value pairs that provide metadata about the response. Think of headers like the label on a package: they tell you important information about the contents (what type, how big, when it expires) without being the actual contents themselves.
Reading Response Headers
After making a fetch request, you access headers through the response.headers object:
fetch('https://api.example.com/data')
.then(response => {
console.log(response.headers.get('content-type'));
console.log(response.headers.get('date'));
console.log(response.headers.get('cache-control'));
return response.json();
});
The get() method retrieves a specific header by name (case-insensitive).
Common Header Values
Here are headers you'll frequently encounter:
content-type: What kind of data is in the response (application/json,text/html,image/png)content-length: Size of the response body in bytesdate: When the server sent the responsecache-control: How browsers should cache this responseetag: A unique identifier for this version of the resourceexpires: When the cached response should be considered stale
Headers guide how your application should handle the response data—whether to cache it, how to parse it, and whether it's still valid.
Key Takeaway: Response headers are metadata sent by the server; use response.headers.get('header-name') to read specific values that tell you how to handle the response content.