Skip to main content

URL Encoding guidelines

The codec is designed for URI and URL components, as well as HTTP Request and Response payloads with the Content-Type of application/x-www-form-urlencoded.

Codec baseline​

The baseline encoding method is Percent Encoding with adaptations for application/x-www-form-urlencoded. Decoding follows the same process in reverse.

For application/x-www-form-urlencoded, spaces ' ' are encoded as plus signs '+', newlines are encoded as '%0D%0A', and all characters except unreserved characters must be percent-encoded with in their UTF-8 representation.

Hierarchical and primitive values​

Hierarchical data structures must be encoded into a single key=value pair. This applies to all top-level values containing a data structure. The encoding involves representing a JSON object or array as a UTF-8 string.

Primitive values are encoded directly without converting to JSON.

Examples​

Token Request as JSON Object
{
grant_type: "vp_token",
scope: "openid did-registry:invite",
vp_token: {
did_registry_invite_credential: ["<signed VP JWT>"]
}
};
Token Request as URL Encoded (POST /auth/token)
grant_type=vp_token
&scope=openid+did-registry%3Ainvite
&vp_token=%7B%22did_registry_invite_credential%22%3A%5B%22%3Csigned+VP+JWT%3E%22%5D%7D

The scope value is a primitive: its spaces become + and its colons %3A. The vp_token value is a JSON object keyed by the DCQL credential-query id (see GET /auth/dcql-queries), so it is serialised to JSON and then percent-encoded.

Encoding objects, arrays and primitives​

Javascript Object
{
obj: {
a: 1,
b: 2
},
arr: [ 1, 2, 3 ],
a: 1,
b: '2',
c: ' '
}
URL Encoded
obj=%7B%22a%22%3A1%2C%22b%22%3A2%7D
&arr=%5B1%2C2%2C3%5D
&a=1
&b=2
&c=+

Javascript example​

The URLSearchParams API can be used for application/x-www-form-urlencoded. All top-level data structures must be JSON stringified in UTF-8 before encoding.

function jsonStringifyNotPrimitive([key, value]) {
if (value == null) {
// filter out null and undefined values
return null;
}
if (typeof value === "object") {
// this includes arrays
return [key, JSON.stringify(value)];
}
return [key, value];
}

function encodeObject(o) {
return new URLSearchParams(
Object.fromEntries(
Object.entries(o).map(jsonStringifyNotPrimitive).filter(Boolean),
),
).toString();
}

This function jsonStringifyNotPrimitive ensures that objects and arrays are JSON stringified, while primitive values are encoded directly. The encodeObject function then creates a URL-encoded string suitable for use in HTTP requests.