Plug.Parsers behaviour

A plug for parsing the request body.

Options

All options supported by Plug.Conn.read_body/2 are also supported here.

Examples

plug Plug.Parsers, parsers: [:urlencoded, :multipart]
plug Plug.Parsers, parsers: [:urlencoded, :json],
                   accept:  ["application/json", "text/*"],
                   json_decoder: Poison

Built-in parsers

Plug ships with the following parsers:

This plug will raise Plug.Parsers.UnsupportedMediaTypeError by default if the request cannot be parsed by any of the given types and the mime type has not been explicity accepted in the :accept option.

Plug.Parsers.RequestTooLargeError will be raised if the request goes over the given limit.

Parsers may raise Plug.Parsers.ParseError if the request has a malformed body.

File handling

If a file is uploaded via any of the parsers, Plug will stream the uploaded contents to a file in a temporary directory, avoiding loading the whole file into memory. For such, it is required that the :plug application is started.

In those cases, the parameter will return a Plug.Upload struct with information about the file and its content type.

You can customize the temporary directory by setting the PLUG_TMPDIR environment variable in your system.

Summary

call(conn, opts)

Callback implementation of Plug.call/2

init(opts)

Callback implementation of Plug.init/1

Functions

call(conn, opts)

Callback implementation of Plug.call/2.

init(opts)

Callback implementation of Plug.init/1.

Callbacks

parse/5

Specs:

Attempt to parse the connection request body given the type, subtype and headers. Returns {:ok, conn} if the parser can handle the given content type, {:halt, conn} otherwise.