Passby.Conn.Query (passby v0.2.0)

Copy Markdown View Source

Decodes x-www-form-urlencoded query strings into Elixir maps.

This mirrors the semantics of Plug.Conn.Query.decode/1 so that Passby handlers receive the exact same conn.query_params/conn.params shape they would get from Bypass, without depending on Plug.

iex> Passby.Conn.Query.decode("filter[name]=Manuel")
%{"filter" => %{"name" => "Manuel"}}

iex> Passby.Conn.Query.decode("tags[]=a&tags[]=b")
%{"tags" => ["a", "b"]}

Rules:

  • foo=bar becomes %{"foo" => "bar"}.
  • A repeated scalar key keeps the last value: foo=1&foo=2 -> %{"foo" => "2"}.
  • A key without a value is treated as an empty string: foo -> %{"foo" => ""}.
  • foo[bar]=baz builds nested maps: %{"foo" => %{"bar" => "baz"}}.
  • foo[]=a&foo[]=b builds a list, preserving order: %{"foo" => ["a", "b"]}.
  • Keys and values are URL-decoded (+ becomes a space, %XX octets are decoded).

Nesting inside lists (for example foo[][bar]=1) is ambiguous and its exact output should not be relied upon, matching Plug.

Summary

Functions

Decodes the given query string into a map.

Functions

decode(query)

@spec decode(binary()) :: %{optional(String.t()) => term()}

Decodes the given query string into a map.