uri API reference
URL parsing, building, and query-string handling (RFC 3986). The shared URL layer the network modules (http, rest, s3, oauth, webdav, ...) build on instead of re-splitting strings and hand-rolling percent-encoding.
parse splits a URL into a Uri (scheme / user / host / port / path / query / fragment); build reassembles one. encode / decode are RFC 3986 percent-encoding (over the encoding library's uri-percent codec), and encodeForm / decodeForm are the application/x-www-form-urlencoded variant (space as "+", the uri-form codec). buildQuery / parseQuery convert between a map of string to string and a key=value&... string using form encoding (the query-string convention). resolve applies a relative reference to a base URL (RFC 3986 section 5), the operation a feed reader or crawler needs to turn a relative link absolute.
Jennifer over strings + encoding + convert + regex (parseQuery runs in encoding.parseForm), no network, so it runs on both binaries. An IPv6 literal host keeps its brackets in host ([::1]) with the port split out; everything else follows RFC 3986.
Import with import "uri.j" as uri;. See the uri guide for prose and examples.
Contents
| Name | What it is |
|---|---|
build | Reassemble a URL string from its parts (the inverse of parse). |
buildQuery | Build a key=value&... query string from a map, form-encoding every key and value (the query-string conven... |
decode | Reverse percent-encoding: %XX triples decode to their byte, and a literal "+" is left as "+" (RFC 3986, n... |
decodeForm | Reverse form-encoding: %XX triples decode to their byte and a "+" decodes to a space (the form-urlencoded... |
encode | Percent-encode a string per RFC 3986 (component encoding): every byte outside the unreserved set... |
encodeForm | Form-encode a string per application/x-www-form-urlencoded: like encode, but a space becomes "+" instea... |
parse | Parse a URL into its parts. |
parseQuery | Parse a key=value&... query string into a map, decoding each component. |
resolve | Resolve a (possibly relative) reference against a base URL, per RFC 3986 section 5: turn "../img.png" plus ... |
Uri | struct: The parts of a parsed URL. |
Functions
build
uri.build(u as Uri)Reassemble a URL string from its parts (the inverse of parse). Components are emitted verbatim (already-encoded); build does not re-encode. An empty query, fragment or port marker ("?", "#", ":") is not kept, and a file: URL with a path and no host is written with "//" ("file:///etc").
u- the parts to assemble
- Returns
string - the URL string
buildQuery
uri.buildQuery(params as map of string to string)Build a key=value&... query string from a map, form-encoding every key and value (the query-string convention: a space becomes "+"). Pairs keep the map's insertion order.
params- the query parameters
- Returns
string - the encoded query string (without a leading "?")
decode
uri.decode(s as string)Reverse percent-encoding: %XX triples decode to their byte, and a literal "+" is left as "+" (RFC 3986, not form encoding - use parseQuery for query strings, which treats "+" as a space).
s- the percent-encoded text
- Returns
string - the decoded text
- Throws
Error - kind "uri" on a malformed "%" escape, or when the decoded bytes are not valid UTF-8
decodeForm
uri.decodeForm(s as string)Reverse form-encoding: %XX triples decode to their byte and a "+" decodes to a space (the form-urlencoded convention). Use this for query-string values; use decode for RFC 3986 path segments where "+" is literal.
s- the form-encoded text
- Returns
string - the decoded text
- Throws
Error - kind "uri" on a malformed "%" escape, or when the decoded bytes are not valid UTF-8
encode
uri.encode(s as string)Percent-encode a string per RFC 3986 (component encoding): every byte outside the unreserved set A-Za-z0-9-._~ becomes %XX, space becomes %20. Safe in any URL position (a path segment, a query value).
s- the text to encode
- Returns
string - the percent-encoded text
encodeForm
uri.encodeForm(s as string)Form-encode a string per application/x-www-form-urlencoded: like encode, but a space becomes "+" instead of "%20". This is the encoding for query-string values and HTML form bodies (buildQuery uses it).
s- the text to encode
- Returns
string - the form-encoded text
parse
uri.parse(raw as string)Parse a URL into its parts. Absent components are "". The authority (user/host/port) is only populated when the URL has a "//" authority, so a scheme-only URL like "mailto:x@y" keeps "x@y" in path. The text before the first ":" is a scheme only when it is a valid scheme name, so "user@host:22/x" is a relative path. The userinfo ends at the last "@". The authority must end after the host or go on with ":" and a port of digits (at most 65535); an IP literal must close its "[". Parsing does not otherwise validate the host.
raw- the URL text
- Returns
Uri - the parsed parts
- Throws
Error - kind "uri" on a port that is not digits or above 65535, an unclosed "[", or text between an IP literal's "]" and the ":" or the path
parseQuery
uri.parseQuery(q as string)Parse a key=value&... query string into a map, decoding each component. A "+" decodes to a space (the form-urlencoded convention), a repeated key keeps its last value, and a bare "key" (no "=") maps to "".
q- the query string (without a leading "?")
- Returns
map of string to string - the decoded parameters
- Throws
Error - kind "uri" on a malformed "%" escape, or when a decoded component is not valid UTF-8; kind "limit" past 10000 pairs
resolve
uri.resolve(base as string, ref as string)Resolve a (possibly relative) reference against a base URL, per RFC 3986 section 5: turn "../img.png" plus "https://h/a/b/page" into "https://h/a/img.png". An absolute reference (with its own scheme) is returned as-is.
base- the absolute base URL
ref- the reference to resolve (absolute or relative)
- Returns
string - the resolved absolute URL
- Throws
Error - kind "uri" when either one has a malformed authority (see
parse)
Structs
Uri
The parts of a parsed URL. Missing components are the empty string (so a relative URL has an empty scheme and host). port is a string ("" when absent) rather than an int, to distinguish "no port" from port 0.
def struct Uri {
scheme as string,
user as string,
host as string,
port as string,
path as string,
query as string,
fragment as string
};scheme- the scheme without the trailing ":" (e.g. "https")
user- the userinfo before the last "@" ("" when absent)
host- the host name or IP literal ("" when absent)
port- the port after ":" ("" when absent)
path- the path, including its leading "/" when present
query- the raw query string without the leading "?"
fragment- the fragment without the leading "#"