Pax.Util.URI (Pax v0.0.1-dev.20251023)

View Source

Summary

Functions

Appends a query to a URI, returning a new URI with the query appended. If the query is nil, it returns the original URI unchanged. If the URI is a string, it parses it first.

Encodes a query string, escaping all that are unreserved, suitable for use in a URL query.

Builds a URL or path with the given query parameters.

Functions

append_query(uri, query)

Appends a query to a URI, returning a new URI with the query appended. If the query is nil, it returns the original URI unchanged. If the URI is a string, it parses it first.

decode_query_string(query)

encode_query_string(query)

Encodes a query string, escaping all that are unreserved, suitable for use in a URL query.

As specified in RFC 3986, section 2.3, the following characters are unreserved:

  • Alphanumeric characters: A-Z, a-z, 0-9
  • ~, _, -, .

with_params(url_or_path, params \\ [])

Builds a URL or path with the given query parameters.

Accepts a URL or path and a keyword list or map of parameters. Handles merging, overriding, and removing parameters, including support for complex values (lists, maps and keyword lists as supported by Plug.Conn.Query).

If a value of [value: v, default: d] option is given, the parameter is omitted if v == d. In this format, :value must come first, otherwise it is treated as a list. If you must provide a list with {:value, _something}, then either make sure it's not first, or pass it as: foo: [value: [value: "bar"]].

Examples

iex> with_params("/test")
"/test"

iex> with_params("/test", foo: "bar")
"/test?foo=bar"

iex> with_params("/test", foo: [1, 2, 3])
"/test?foo[]=1&foo[]=2&foo[]=3"

iex> with_params("/test?foo=bar", foo: nil)
"/test"

iex> with_params("/test", foo: [value: "bar", default: "bar"])
"/test"

iex> with_params("/test", foo: [value: "baz", default: "bar"])
"/test?foo=baz"