View Source Tesla.Middleware.FormUrlencoded (tesla v1.13.2)
Send request body as application/x-www-form-urlencoded
.
Performs encoding of body
from a Map
such as %{"foo" => "bar"}
into
URL-encoded data.
Performs decoding of the response into a map when urlencoded and content-type
is application/x-www-form-urlencoded
, so "foo=bar"
becomes
%{"foo" => "bar"}
.
Examples
defmodule Myclient do
def client do
Tesla.client([
{Tesla.Middleware.FormUrlencoded,
encode: &Plug.Conn.Query.encode/1,
decode: &Plug.Conn.Query.decode/1}
])
end
end
client = Myclient.client()
Myclient.post(client, "/url", %{key: :value})
Options
:decode
- decoding function, defaults toURI.decode_query/1
:encode
- encoding function, defaults toURI.encode_query/1
Nested Maps
Natively, nested maps are not supported in the body, so
%{"foo" => %{"bar" => "baz"}}
won't be encoded and raise an error.
Support for this specific case is obtained by configuring the middleware to
encode (and decode) with Plug.Conn.Query
defmodule Myclient do
def client do
Tesla.client([
{Tesla.Middleware.FormUrlencoded,
encode: &Plug.Conn.Query.encode/1,
decode: &Plug.Conn.Query.decode/1}
])
end
end
client = Myclient.client()
Myclient.post(client, "/url", %{key: %{nested: "value"}})
Summary
Functions
Decode response body as querystring.
It is used by Tesla.Middleware.DecodeFormUrlencoded
.
Encode response body as querystring.
It is used by Tesla.Middleware.EncodeFormUrlencoded
.