API Fundamentals
/Intermediate
HTTP Headers
Definition
Key-value pairs sent in both HTTP requests and responses that provide crucial metadata about the transaction, the client, the server, or the body payload.
Explain Like I'm New
Headers are the invisible 'settings' of an HTTP request. They tell the server things like: 'I only speak Spanish' (Accept-Language), 'I am using Google Chrome on a Mac' (User-Agent), or 'Here is my secret password' (Authorization).
Real World Example
A mobile API rejecting a request because the `Content-Type` header was incorrectly set to `text/plain` instead of `application/json`.
Common Use Cases
- •Authentication
- •Content negotiation
- •Caching policies
- •CORS security
Terminal Output
bash / terminal
/* Common Request Headers sent by your Browser: */
Authorization: Bearer xxxxxxxx.yyyyyyyy.zzzzzzzz
Content-Type: application/json
Accept: application/json // Tell the server I expect JSON back
Accept-Language: en-US,en;q=0.9 // I prefer English
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
/* Common Response Headers sent by the Server: */
Content-Type: text/html; charset=UTF-8
Cache-Control: public, max-age=3600 // Tell the browser to cache this for 1 hour
Set-Cookie: session_id=123; HttpOnly; Secure // Force the browser to save a cookie
Access-Control-Allow-Origin: * // CORS: Let any website hit this API
Interview Questions
basic
- Which header is commonly used to pass a JWT (JSON Web Token) to the server for authentication?
intermediate
- What is the purpose of the `User-Agent` header?