45 Mins

6. HTTP Deep Dive — Headers, CORS, & HTTPS

Foundation

HTTP Deep Dive

Now that you understand the basics of HTTP (requests, responses, methods, status codes), let's go deeper into the hidden metadata that powers the web: Headers, CORS, and HTTPS.

Prerequisite:
Before this module, you should have completed ba-1 (How the Web Works). You understand GET/POST, 200/404/500, and the request-response cycle.

What Are HTTP Headers?

Headers are key-value pairs sent with every request and response. They are invisible to users but critical for servers and browsers. Think of them as the envelope of your letter — they don't contain the message, but they tell the postal service how to deliver it.

Here's what headers look like in a real request:

Text
GET /users/me HTTP/1.1
Host: api.voidx.io
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...
Accept: application/json
Accept-Language: en-US,en;q=0.9
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Cache-Control: no-cache

Let's break down the most important headers you'll use daily:

  • Content-Type: Tells the server what format the request body is in. Values: application/json, text/html, multipart/form-data (for file uploads).
  • Authorization: Carries authentication credentials. Almost always Bearer <token> for modern APIs.
  • Accept: Tells the server what format the client wants back. Accept: application/json means give me JSON, please.
  • User-Agent: Identifies what browser or client is making the request. Used for analytics and compatibility.
  • Cache-Control: Instructs the browser how long to store the response. Cache-Control: max-age=3600 means keep this for 1 hour.

CORS — Cross-Origin Resource Sharing

Imagine you're logged into your bank's website at https://mybank.com. A malicious site https://evil.com tries to send a request to your bank's API to transfer money. Without CORS, that request would succeed — the browser would just send your bank cookies along with the request.

CORS is the security system that prevents this.

When a webpage at https://frontend.com tries to call an API at https://api.backend.com, the browser first sends a preflight request (using the OPTIONS method) asking: "Does api.backend.com allow requests from frontend.com?"

Text
OPTIONS /api/users HTTP/1.1
Origin: https://frontend.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type

The server must respond with specific headers to grant permission:

Text
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://frontend.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Allow-Credentials: true

The golden rule: Never use Access-Control-Allow-Origin: * in production. Whitelist only your specific frontend domain.

HTTPS — Why Encryption Matters

HTTP sends all data as plain text. If you're on public Wi-Fi (coffee shop, airport, hotel), anyone on the same network can see every request and response — including passwords, credit cards, and private messages.

HTTPS wraps every HTTP conversation inside TLS (Transport Layer Security) encryption. Even if someone intercepts the traffic, they see nothing but scrambled, unreadable ciphertext.

Free HTTPS for everyone: Services like Let's Encrypt provide free TLS certificates. Platforms like Railway, Render, and Vercel automatically provision HTTPS for every deployment — zero configuration required.

How to spot if a site is secure: Look for the padlock icon in your browser's address bar. If it's not there, do NOT enter any passwords or payment information.

Common Headers You'll Use

Request Headers

Authorization: Bearer <token>
Content-Type: application/json
Accept: application/json
Origin: https://myapp.com

Response Headers

Access-Control-Allow-Origin: https://myapp.com
Cache-Control: max-age=3600
Content-Type: application/json
X-Request-ID: req_abc123

Pro tip: Add a unique X-Request-ID header to every response. When something breaks, you can search your logs by that ID to trace exactly what happened for that specific user.

Knowledge Check

Ready to test your understanding of 6. HTTP Deep Dive — Headers, CORS, & HTTPS?

Back