Plug v1.1.3 Plug.Parsers behaviour

A plug for parsing the request body.

This module also specifies a behaviour that all the parsers to be used with Plug should adopt.

Options

  • :parsers - a set of modules to be invoked for parsing. These modules need to implement the behaviour outlined in this module.

  • :pass - an optional list of MIME type strings that are allowed to pass through. Any mime not handled by a parser and not explicitly listed in :pass will raise UnsupportedMediaTypeError. For example:

    • ["*/*"] - never raises
    • ["text/html", "application/*"] - doesn’t raise for those values
    • [] - always raises (default)

All options supported by Plug.Conn.read_body/2 are also supported here (for example the :length option which specifies the max body length to read).

Examples

plug Plug.Parsers, parsers: [:urlencoded, :multipart]
plug Plug.Parsers, parsers: [:urlencoded, :json],
                   pass:  ["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 with the :pass option.

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

Parsers may raise a 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 in order to avoid loading the whole file into memory. For such, the :plug application needs to be started in order for file uploads to work. More details on how the uploaded file is handled can be found in the documentation for Plug.Upload.

When a file is uploaded, the request parameter that identifies that file will be a Plug.Upload struct with information about the uploaded file (e.g. filename and content type) and about where the file is stored.

The temporary directory where files are streamed to can be customized by setting the PLUG_TMPDIR environment variable on the host system. If PLUG_TMPDIR isn’t set, Plug will look at some environment variables which usually hold the value of the system’s temporary directory (like TMPDIR or TMP). If no value is found in any of those variables, /tmp is used as a default.

Summary

Functions

Callback implementation for Plug.call/2

Callback implementation for Plug.init/1

Callbacks

Attempts to parse the connection’s request body given the content-type type and subtype and the headers. Returns

Functions

call(conn, opts)

Callback implementation for Plug.call/2.

init(opts)

Callback implementation for Plug.init/1.

Callbacks

parse(arg0, type, subtype, headers, opts)

Specs

parse(Plug.Conn.t, type :: binary, subtype :: binary, headers :: Keyword.t, opts :: Keyword.t) ::
  {:ok, Plug.Conn.params, Plug.Conn.t} |
  {:error, :too_large, Plug.Conn.t} |
  {:next, Plug.Conn.t}

Attempts to parse the connection’s request body given the content-type type and subtype and the headers. Returns:

  • {:ok, conn} if the parser is able to handle the given content-type
  • {:next, conn} if the next parser should be invoked
  • {:error, :too_large, conn} if the request goes over the given limit