A JSON Encoding for HTTP Header Field Valuesgreenbytes GmbHHafenweg 16MuensterNW48155Germanyjulian.reschke@greenbytes.dehttp://greenbytes.de/tech/webdav/
Applications
HTTPJSONHeader Field Value
This document establishes a convention for use of JSON-encoded field
values in HTTP header fields.
Distribution of this document is unlimited. Although this is not a work
item of the HTTPbis Working Group, comments should be sent to the
Hypertext Transfer Protocol (HTTP) mailing list at ietf-http-wg@w3.org,
which may be joined by sending a message with subject
"subscribe" to ietf-http-wg-request@w3.org.
Discussions of the HTTPbis Working Group are archived at
.
XML versions and latest edits for this document
are available from .
Defining syntax for new HTTP header fields (, Section 3.2) is non-trivial. Among the commonly encountered
problems are:
There is no common syntax for complex field values. Several well-known
header fields do use a similarly looking syntax, but it is hard to write
generic parsing code that will both correctly handle valid field values
but also reject invalid ones.
The HTTP message format allows header fields to repeat, so field syntax
needs to be designed in a way that these cases are either meaningful,
or can be unambiguously detected and rejected.
HTTP/1.1 does not define a character encoding scheme (, Section 2), so header fields are either stuck with US-ASCII
(), or need out-of-band information
to decide what encoding scheme is used. Furthermore, APIs
usually assume a default encoding scheme in order to map from
octet sequences to strings (for instance,
uses the IDL type "ByteString", effectively resulting in the
ISO-8859-1 character encoding scheme being used).
(See Section 8.3.1 of
for a summary of considerations for new header fields.)
This specification addresses the issues listed above by defining both a generic
JSON-based () data model and a concrete
wire format that can be used in definitions of new header fields.
In HTTP, header fields with the same field name can occur multiple times
within a single message (Section 3.2.2 of ).
When this happens, recipients are allowed to combine the field values using
commas as delimiter. This rule matches nicely JSON's array format
(Section 5 of ). Thus, the basic data model
used here is the JSON array.
Header field definitions that need only a single value can restrict
themselves to arrays of length 1, and are encouraged to define error
handling in case more values are received (such as "first wins", "last wins",
or "abort with fatal error message").
JSON arrays are mapped to field values by creating a sequence of
serialized member elements, separated by commas and optionally whitespace. This
is equivalent to using the full JSON array format, while leaving out
the "begin-array" ('[') and "end-array" (']') delimiters.
Characters in JSON strings that are not allowed or discouraged in HTTP
header field values — that is, not in the "VCHAR" definition —
need to be represented using JSON's "backslash" escaping mechanism
(, Section 7).
The control characters CR, LF, and HTAB do not appear inside JSON
strings, but can be used outside (line breaks, indentation etc). These characters
need to be either stripped or replaced by space characters (ABNF "SP").
Formally, using the HTTP specification's ABNF extensions defined in
Section 7 of :
To map a JSON array to an HTTP header field value, process each array
element separately by:
generating the JSON representation,stripping all JSON control characters (CR, HTAB, LF), or replacing
them by space ("SP") characters,replacing all remaining non-VSPACE characters by the equivalent
backslash-escape sequence (, Section 7).
The resulting list of strings is transformed into an HTTP field value
by combining them using comma (%x2C) plus optional SP as delimiter,
and encoding the resulting string into an octet sequence using the
US-ASCII character encoding scheme ().
To map a set of HTTP header field instances to a JSON array:
combine all header field instances into a single field as per
Section 3.2.2 of ,add a leading begin-array ("[") octet and a trailing end-array ("]") octet, thenrun the resulting octet sequence through a JSON parser.
The result of the parsing operation is either an error (in which case
the header field values needs to be considered invalid), or a JSON array.
Explain what a definition of a new header field needs to do precisely
to use this format
This section shows how some of the existing HTTP header fields would look
like if they would use the format defined by this specification.
"Content-Length" is defined in Section 3.3.2 of , with the field value's ABNF being:
So the field value is similar to a JSON number (, Section 6).
Content-Length is restricted to a single field instance, as it doesn't use
the list production (as per Section 3.2.2 of ).
However, in practice multiple instances do occur, and the definition of
the header field does indeed discuss how to handle these cases.
If Content-Length was defined using the JSON format discussed here, the
ABNF would be something like:
...and the prose definition would:
restrict all numbers to be non-negative integers without fractions, andrequire that the array of values is of length 1
(but allow the case where the array is longer, but all members represent
the same value)
Content-Disposition field values, defined in , consist of
a "disposition type" (a string), plus multiple parameters, of which at least
one ("filename") sometime needs to carry non-ASCII characters.
For instance, the first example in Section 5 of :
has a disposition type of "Attachment", with filename parameter value
"example.html". A JSON representation of this information might be:
which would translate to a header field value of:
The third example in Section 5 of
uses a filename parameter containing non-US-ASCII characters:
Note that in this case, the "filename*" parameter uses the encoding defined in
, representing a filename starting with the Unicode
character U+20AC (EURO SIGN), followed by " rates". If the definition
of Content-Disposition would have used the format proposed here, the
workaround involving the "parameter*" syntax would not have been needed at
all.
The JSON representation of this value could then be:
The WWW-Authenticate is defined in Section 4.1 of as a list of "challenges":
...where a challenge consists of a scheme with optional parameters:
An example for a complex header field value given in the definition of
the header field is:
A possible JSON representation of this field value would be the array below:
...which would translate to a header field value of:
This approach uses a default of "JSON array", using implicit array markers.
An alternative would be a default of "JSON object". This would simplify
the syntax for non-list-typed haeders, but all the benefits of having the
same data model for both types of header fields would be gone.
A hybrid approach might make sense, as long as it doesn't require any
heuristics on the recipient's side.
Use of generic libs vs compactness of field values..
This JSON-based syntax will only apply to newly introduced
header fields, thus backwards compatibility is not a problem. That being
said, it is conceivable that there is existing code that might trip over
double quotes not being used for HTTP's quoted-string syntax (Section 3.2.6 of ).
TBD, mention migration path to message format that is robust wrt UTF-8, or other binary encodings of JSONTBDASCII format for network interchangeAugmented BNF for Syntax Specifications: ABNFThe JavaScript Object Notation (JSON) Data Interchange FormatHypertext Transfer Protocol (HTTP/1.1): Message Syntax and RoutingAdobe Systems Incorporatedfielding@gbiv.comgreenbytes GmbHjulian.reschke@greenbytes.deHypertext Transfer Protocol (HTTP/1.1): Semantics and ContentAdobe Systems Incorporatedfielding@gbiv.comgreenbytes GmbHjulian.reschke@greenbytes.deInformation technology -- 8-bit single-byte coded graphic character sets -- Part 1: Latin alphabet No. 1International Organization for StandardizationCharacter Set and Language Encoding for Hypertext Transfer Protocol (HTTP) Header Field ParametersUse of the Content-Disposition Header Field
in the Hypertext Transfer Protocol (HTTP)Terminology Used in Internationalization in the IETFHypertext Transfer Protocol (HTTP/1.1): AuthenticationXMLHttpRequest Level 1
Latest version available at
.
Editorial fixes + working on the TODOs.