Plug.Conn.Query

Conveniences for decoding and encoding url encoded queries

Plug allows a developer to build query strings that maps to Elixir structures in order to make manipulation of such structures easier on the server side. Here are some examples:

iex> decode("foo=bar")["foo"]
"bar"

If a value is given more than once, the last value wins:

iex> decode("foo=bar&foo=baz")["foo"]
"baz"

Nested structures can be created via [key]:

iex> decode("foo[bar]=baz")["foo"]["bar"]
"baz"

Lists are created with []:

iex> decode("foo[]=bar&foo[]=baz")["foo"]
["bar", "baz"]

Encoding Dicts:

iex> encode(%{foo: "bar", baz: "bat"})
"baz=bat&foo=bar"

Encoding keyword lists preserves field order:

iex> encode([foo: "bar", baz: "bat"])
"foo=bar&baz=bat"

Encoding keyword lists with duplicate keys, first one wins:

iex> encode([foo: "bar", foo: "bat"])
"foo=bar"

Encoding named lists:

iex> encode(%{foo: ["bar", "baz"]})
"foo[]=bar&foo[]=baz"

Encoding nested structures:

iex> encode(%{foo: %{bar: "baz"}})
"foo[bar]=baz"

Summary

decode(query, initial \\ %{})

Decodes the given binary

decode_pair(arg1, acc)

Decodes the given tuple and store it in the accumulator. It parses the key and stores the value into te current accumulator

encode(dict)

Encodes the given dict

Functions

decode(query, initial \\ %{})

Decodes the given binary.

decode_pair(arg1, acc)

Decodes the given tuple and store it in the accumulator. It parses the key and stores the value into te current accumulator.

Parameters lists are added to the accumulator in reverse order, so be sure to pass the parameters in reverse order.

encode(dict)

Encodes the given dict.