HTTP Headers
HTTP headers are key-value pairs sent between the client (browser) and server during an HTTP request or response. They carry metadata about the request or response.
Types of HTTP Headers
- Request Headers (sent by the client to the server)
- Response Headers (sent by the server to the client)
Request Headers
Request headers are key-value pairs sent by the client (browser or app) to the server when making an HTTP request. They carry metadata about the request, such as the browser type, authentication, content type, and more.
- Host: The domain name (e.g., vaidikalaya.com)
- User-Agent: Info about the browser/app making the request
- Accept: Tells the server what content type is expected (application/json, text/html, etc.)
- Authorization: Sends credentials (e.g., Bearer token or Basic auth)
- Content-Type: The format of the data being sent (application/json, form-data)
- Cookie: Sends cookies to the server
Example in Javascript
Note
Request headers are automatically added by browsers or can be manually set in custom requests (e.g., using fetch, Axios, Postman).
Headers Not Added Automatically by the Browser: Authorization (Bearer tokens, API keys), Custom Headers (X-CSRF-TOKEN, X-Requested-With), Content-Type
Response Headers
Response headers are key-value pairs sent by the server back to the client (browser, Postman, etc.) along with the response content. They provide information about the response and how the client should handle it.
- Content-Type: Describes the type of data returned (e.g., text/html, application/json)
- Content-Length: Size of the response body in bytes
- Set-Cookie: Sends cookies to be stored on the client
- Cache-Control: Controls caching behavior (no-cache, max-age, etc.)
- ETag: Used for cache validation (efficient content reloading)
- Location: Used for redirection (e.g., Location: /login)
- Access-Control-Allow-Origin: CORS header: specifies who can access this resource
- Expires: Expiration date of the cached response
- Last-Modified: Date when resource was last changed
- X-RateLimit-Limit/Remaining: Used in APIs to indicate request rate limits
- Content-Encoding: Indicates compression (e.g., gzip)
Example
HTTP Ports
When your browser connects to a website, it uses a port number along with the IP address to reach the correct service on the server.
Default Http Ports
There are some default ports that browsers use if you don’t explicitly provide a port in the URL.
(browser, Postman, etc.) along with the response content. They provide information about the response and how the client should handle it.- 80 (HTTP): Default port for unsecured web traffic (http://)
- 443 (HTTPS): Default port for secure web traffic using SSL/TLS (https://)
Example
- http://example.com → uses port 80 internally
- https://example.com → uses port 443 internally
- http://localhost:8000 → uses port 8000 (custom)y
Other Common Ports in Web Development:
- 3000: Default port for React/Node.js
- 8000: Default port for Laravel or Python (Django/Flask)
- 8080: Alternative HTTP port, often used when 80 is blocked
- 3306: MySQL database server (not HTTP, but used in backend)
- 5432: PostgreSQL database server
- 27017: MongoDB server