Plug.Conn.Query

Plug.Conn.Query

Conveniences for decoding and encoding url encoded queries.

Plug allows a developer to build query strings that map 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 takes precedence:

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"]

Maps can be encoded:

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

Encoding keyword lists preserves the order of the fields:

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

When encoding keyword lists with duplicate keys, the key that comes first takes precedence:

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

Functions

decode(query, initial \\ %{})

Decodes the given binary

decode_pair(arg, acc)

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

encode(kv, encoder \\ &to_string/1)

Encodes the given map or list of tuples

Functions

decode(query, initial \\ %{})

Decodes the given binary.

decode_pair(arg, acc)

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

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

encode(kv, encoder \\ &to_string/1)

Encodes the given map or list of tuples.

© 2013 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/plug/Plug.Conn.Query.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部