tesla v1.3.0 Tesla.Middleware.FormUrlencoded View Source

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"}.

Example usage

defmodule Myclient do
  use Tesla

  plug Tesla.Middleware.FormUrlencoded
end

Myclient.post("/url", %{key: :value})

Options

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
  use Tesla

  plug Tesla.Middleware.FormUrlencoded,
    encode: &Plug.Conn.Query.encode/1,
    decode: &Plug.Conn.Query.decode/1
end

Myclient.post("/url", %{key: %{nested: "value"}})