Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
API Basics APIs APIs From Zero to Real-World API Testing & Automation HTTP Module 1 — API Fundamentals

HTTP Headers Explained — The Metadata of Every Request and Response

Reviewed & accurate
AI Summary

What You'll Learn

  • What HTTP headers are and why they exist
  • The headers you will use in every API call
  • Request headers vs response headers
  • How content negotiation works via Accept and Content-Type

Why This Matters

Headers carry the metadata that surrounds every HTTP message. Without Content-Type, the server does not know if your body is JSON or XML. Without Authorization, it does not know who you are. Without Accept, it does not know what format to respond in. If you do not understand headers, you cannot debug "400 Bad Request" or "415 Unsupported Media Type" errors.

Simple Explanation

A header is a single line at the top of an HTTP message, formatted as Name: Value. Headers come right after the request line (or status line) and before the blank line that ends the headers section.

GET /users/123 HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer eyJhbGciOi...

[blank line]

Each header carries one piece of metadata. Multiple headers stack together to describe the request.

Header Naming Convention

  • Names are case-insensitive: Content-Type, content-type, and CONTENT-TYPE are all the same.
  • Convention: hyphen-separated, title-case (Content-Type, User-Agent).
  • Custom headers often start with X- (e.g., X-Request-Id), though this convention is no longer required by RFC 6648.

Request Headers You Will Use Daily

Host

The domain name of the server. Required in HTTP/1.1 because one IP can serve many domains.

Host: api.example.com

Accept

Tells the server what content types the client can handle in the response. The server uses this to choose the response format.

Accept: application/json
Accept: application/xml
Accept: */*

Content-Type

Tells the server what format the request body is in. Required whenever there is a body (POST, PUT, PATCH).

Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
Content-Type: multipart/form-data

Authorization

Carries credentials that prove who the client is. Most common format is Bearer token:

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Covered in depth in Module 5.

User-Agent

Identifies the client software. Servers use it for analytics and sometimes for routing.

User-Agent: curl/7.81.0
User-Agent: MyApp/1.0 (https://example.com)

Accept-Encoding

Tells the server the client can accept compressed responses (gzip, br). Most clients set this automatically.

Accept-Encoding: gzip, deflate

Response Headers You Will Use Daily

Content-Type

Tells the client what format the response body is in. Should always match what the client requested via Accept (or the server's default).

Content-Type: application/json; charset=utf-8

Content-Length

The size of the response body in bytes. Lets the client know when the response is complete.

Content-Length: 247

Cache-Control

Tells caches (browser, CDN, proxy) how long they can store the response.

Cache-Control: max-age=3600, public
Cache-Control: no-cache, no-store, must-revalidate

ETag

A hash of the resource's current state. The client can send it back as If-None-Match on the next request. If the resource has not changed, the server returns 304 Not Modified instead of resending the body.

ETag: "abc123"
If-None-Match: "abc123"

Location

Used with 201 Created (and 3xx redirects) to point to the new resource's URL.

Location: https://api.example.com/users/123

Retry-After

Used with 429 Too Many Requests or 503 Service Unavailable to tell the client how long to wait before retrying.

Retry-After: 60

Content Negotiation — How Accept and Content-Type Work Together

The client sends Accept to say what it wants. The server sends Content-Type to say what it actually returned. These should agree.

# Client asks for JSON
GET /users/123 HTTP/1.1
Accept: application/json

# Server agrees and returns JSON
HTTP/1.1 200 OK
Content-Type: application/json

{"id": 123, "name": "Anita"}

If the client asks for XML and the server only supports JSON, the server may return 406 Not Acceptable or just return JSON anyway (most APIs do the latter).

Common Mistakes

  1. Forgetting Content-Type on a POST/PUT body. The server has no way to know your body is JSON, so it returns 415 Unsupported Media Type or parses it as form data.
  2. Sending the wrong Accept header. If you set Accept: text/html on an API that only returns JSON, you may get an error or HTML error page.
  3. Putting credentials in custom headers. Use Authorization for tokens. Custom headers can be stripped by proxies or load balancers.
  4. Hard-coding Content-Type: application/json for form posts. Forms use application/x-www-form-urlencoded or multipart/form-data. Wrong type = silent data loss.
  5. Treating headers as case-sensitive. They are not. content-type and Content-Type are equivalent, but stick with one style for readability.

Practical Exercise (5 minutes)

Run this cURL command and inspect the response headers:

curl -I -H "Accept: application/json" https://jsonplaceholder.typicode.com/posts/1

The -I flag fetches only headers. Note:

  1. The value of Content-Type in the response.
  2. The value of Cache-Control (or its absence).
  3. The value of Content-Length.
  4. Whether ETag is set.

Mini Challenge

Send the same request twice to JSONPlaceholder. First, set Accept: application/xml. Then set Accept: text/html. Does the server respect the header and change its response format? (Some do, some don't — observing this teaches you how content negotiation actually works in practice.)

Key Takeaways

  • Headers are key-value metadata at the top of every HTTP message.
  • Key request headers: Host, Accept, Content-Type, Authorization, User-Agent.
  • Key response headers: Content-Type, Content-Length, Cache-Control, ETag, Location, Retry-After.
  • Always set Content-Type when sending a body. Always set Accept when you want a specific response format.
  • Headers are case-insensitive but conventionally use title-case with hyphens.
Course continuity
Previously: Lesson 05 covered status codes.
Today: You learned the headers that travel alongside every request and response.
Next: In lesson 07 — Request Body and Response Body, you'll learn what goes inside the body itself.

FAQ

What is the difference between a header and a cookie?

A cookie is a specific kind of header. The server sends Set-Cookie in the response; the browser stores the cookie and sends it back as a Cookie header on future requests. Cookies are mostly used by browsers for sessions. APIs typically use Authorization headers instead, because cookies do not work well across domains.

Can I create my own custom headers?

Yes. Any header name not in the standard list is treated as custom. Many APIs use custom headers for things like request IDs (X-Request-Id) or rate-limit info (X-RateLimit-Remaining). Just be aware that proxies may strip unknown headers, so test through your full infrastructure.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments