Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Created January 14, 2025 21:57
Show Gist options
  • Save jonchurch/7e54e22866a3cedad34f2218ed18931c to your computer and use it in GitHub Desktop.
Save jonchurch/7e54e22866a3cedad34f2218ed18931c to your computer and use it in GitHub Desktop.
ABNF Cheatsheet for HTTP Specs

ABNF/HTTP Syntax Cheatsheet

Basic ABNF Operators

Operator Meaning
= Defines a rule. Example: rule-name = value
/ Logical OR (choice). Example: A / B means "A or B".
* Zero or more occurrences. Example: *DIGIT means "0 or more DIGITs".
1* One or more occurrences. Example: 1*DIGIT means "1 or more DIGITs".
[ ] Optional (zero or one occurrence). Example: [DIGIT] means "optional DIGIT".
() Grouping. Example: (A / B) SP means "A or B followed by a space".
# Comma-separated list. Example: #DIGIT means "1+ DIGITs, separated by commas".
ALPHA Any alphabetic character (A-Z, a-z).
DIGIT Any numeric digit (0-9).
HEXDIG Any hexadecimal digit (0-9, A-F).

Common HTTP/ABNF Terms

Term Definition
SP Space (U+0020).
HTAB Horizontal tab (U+0009).
CR Carriage return (\r, U+000D).
LF Line feed (\n, U+000A).
CRLF Carriage return + line feed (\r\n).
OWS Optional Whitespace (*( SP / HTAB )).
BWS Bad Whitespace (used for backward compatibility, trimmed during parsing).
token Any sequence of visible ASCII characters except separators.
quoted-string A string enclosed in double quotes ("), allowing escapes with \.
field-name A token identifying a header field. Example: Content-Type.
field-value The value of a header field, which can include tokens or a quoted string.

HTTP-Specific Syntax Examples

Construct ABNF Definition Meaning
OWS *( SP / HTAB ) Zero or more spaces or tabs (optional whitespace).
header-field field-name ":" OWS field-value OWS Header field definition (name, colon, value).
method token HTTP request method (e.g., GET, POST).
request-line method SP request-target SP HTTP-version CRLF First line of an HTTP request.
status-line HTTP-version SP status-code SP reason-phrase CRLF First line of an HTTP response.
absolute-URI Defined in RFC 3986. Full URI (scheme, host, path, etc.).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment