{ ILoveJS }

HTTP Headers Cheatsheet

http

Essential HTTP headers for web developers — security, caching, CORS.

6 sections · 43 items

Request Headers

Accept
Accept: <MIME_type>/<MIME_subtype>

Specifies the media types the client can process in the response

typescript
Accept: application/json, text/html;q=0.9, */*;q=0.8
Authorization
Authorization: <auth-scheme> <credentials>

Contains credentials to authenticate the client with the server

typescript
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type (Request)
Content-Type: <media-type>; charset=<charset>

Indicates the media type of the request body being sent

typescript
Content-Type: application/json; charset=utf-8
Cookie
Cookie: <name>=<value>; <name2>=<value2>

Sends stored cookies from the client to the server

typescript
Cookie: sessionId=abc123; theme=dark; lang=en
User-Agent
User-Agent: <product>/<version> (<system-info>)

Identifies the client application, operating system, and version

typescript
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0
If-Modified-Since
If-Modified-Since: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT

Makes request conditional; returns 304 if resource unchanged since date

typescript
If-Modified-Since: Wed, 21 Oct 2024 07:28:00 GMT
If-None-Match
If-None-Match: "<etag_value>"

Makes request conditional; returns 304 if ETag matches cached version

typescript
If-None-Match: "33a64df551425fcc55e4d42a148795d9"
Origin
Origin: <scheme>://<hostname>:<port>

Indicates the origin of the request; used for CORS and security

typescript
Origin: https://www.example.com
Referer
Referer: <url>

Contains the URL of the page that linked to the current request

typescript
Referer: https://www.google.com/search?q=http+headers

Response Headers

Content-Type (Response)
Content-Type: <media-type>; charset=<charset>; boundary=<boundary>

Indicates the media type of the response body

typescript
Content-Type: text/html; charset=UTF-8
Content-Length
Content-Length: <length-in-bytes>

Indicates the size of the response body in bytes

typescript
Content-Length: 3495
Cache-Control
Cache-Control: <directive>, <directive>

Specifies caching directives for both requests and responses

typescript
Cache-Control: public, max-age=31536000, immutable
ETag
ETag: "<etag_value>" | W/"<etag_value>"

Unique identifier for a specific version of a resource

typescript
ETag: "33a64df551425fcc55e4d42a148795d9f8f"
Last-Modified
Last-Modified: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT

Date and time the resource was last modified on the server

typescript
Last-Modified: Tue, 15 Nov 2024 12:45:26 GMT
Location
Location: <url>

Indicates the URL to redirect a page to; used with 3xx or 201 status

typescript
Location: https://www.example.com/new-page
Set-Cookie
Set-Cookie: <name>=<value>; <attributes>

Sends a cookie from the server to be stored on the client

typescript
Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Strict; Max-Age=86400
Vary
Vary: <header-name>, <header-name>

Indicates which request headers affect the response content for caching

typescript
Vary: Accept-Encoding, Accept-Language, Origin

Security Headers

Content-Security-Policy
Content-Security-Policy: <directive> <source>; <directive> <source>

Controls resources the browser is allowed to load for the page

typescript
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'
X-Frame-Options
X-Frame-Options: DENY | SAMEORIGIN

Prevents the page from being embedded in iframes to stop clickjacking

typescript
X-Frame-Options: DENY
X-Content-Type-Options
X-Content-Type-Options: nosniff

Prevents browsers from MIME-sniffing a response from declared content-type

typescript
X-Content-Type-Options: nosniff
Strict-Transport-Security
Strict-Transport-Security: max-age=<seconds>; includeSubDomains; preload

Forces browsers to use HTTPS for all future requests to the domain

typescript
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Referrer-Policy
Referrer-Policy: <policy>

Controls how much referrer information is sent with requests

typescript
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy
Permissions-Policy: <feature>=(<allowlist>)

Controls which browser features and APIs can be used on the page

typescript
Permissions-Policy: geolocation=(self), camera=(), microphone=()

CORS Headers

Access-Control-Allow-Origin
Access-Control-Allow-Origin: <origin> | *

Specifies which origins are allowed to access the resource

typescript
Access-Control-Allow-Origin: https://www.example.com
Access-Control-Allow-Methods
Access-Control-Allow-Methods: <method>, <method>

Specifies allowed HTTP methods when accessing the resource

typescript
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers
Access-Control-Allow-Headers: <header-name>, <header-name>

Specifies allowed request headers for cross-origin requests

typescript
Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With
Access-Control-Allow-Credentials
Access-Control-Allow-Credentials: true

Indicates whether the response can be shared when credentials are included

typescript
Access-Control-Allow-Credentials: true
Access-Control-Max-Age
Access-Control-Max-Age: <delta-seconds>

Indicates how long preflight request results can be cached

typescript
Access-Control-Max-Age: 86400
Access-Control-Expose-Headers
Access-Control-Expose-Headers: <header-name>, <header-name>

Specifies which headers can be exposed to JavaScript in the browser

typescript
Access-Control-Expose-Headers: X-Custom-Header, Content-Length

Common Cache-Control Directives

max-age
Cache-Control: max-age=<seconds>

Maximum time a resource is considered fresh in seconds

typescript
Cache-Control: max-age=3600
no-cache
Cache-Control: no-cache

Must revalidate with server before using cached response

typescript
Cache-Control: no-cache
no-store
Cache-Control: no-store

Response must not be stored in any cache; always fetch fresh

typescript
Cache-Control: no-store
public
Cache-Control: public

Response can be cached by any cache including CDNs and proxies

typescript
Cache-Control: public, max-age=86400
private
Cache-Control: private

Response is intended for a single user and must not be cached by shared caches

typescript
Cache-Control: private, max-age=3600
immutable
Cache-Control: immutable

Indicates the response body will not change over time

typescript
Cache-Control: public, max-age=31536000, immutable
stale-while-revalidate
Cache-Control: stale-while-revalidate=<seconds>

Serve stale content while revalidating in the background

typescript
Cache-Control: max-age=3600, stale-while-revalidate=86400

Content-Security-Policy Directives

default-src
Content-Security-Policy: default-src <source>

Fallback policy for all resource types not explicitly defined

typescript
Content-Security-Policy: default-src 'self'
script-src
Content-Security-Policy: script-src <source>

Controls valid sources for JavaScript execution

typescript
Content-Security-Policy: script-src 'self' 'unsafe-inline' https://cdn.example.com
style-src
Content-Security-Policy: style-src <source>

Controls valid sources for stylesheets

typescript
Content-Security-Policy: style-src 'self' 'unsafe-inline'
img-src
Content-Security-Policy: img-src <source>

Controls valid sources for images and favicons

typescript
Content-Security-Policy: img-src 'self' data: https:
connect-src
Content-Security-Policy: connect-src <source>

Controls URLs that can be loaded using fetch, XHR, and WebSocket

typescript
Content-Security-Policy: connect-src 'self' https://api.example.com wss://ws.example.com
frame-ancestors
Content-Security-Policy: frame-ancestors <source>

Specifies valid parents that may embed the page in frames

typescript
Content-Security-Policy: frame-ancestors 'self' https://trusted.com
upgrade-insecure-requests
Content-Security-Policy: upgrade-insecure-requests

Instructs browsers to upgrade HTTP requests to HTTPS

typescript
Content-Security-Policy: upgrade-insecure-requests

Related Content