saxon v0.1.0 Saxon

Saxon is a highly opinionated XML request parser for Plug. It only supports parsing the HTTP requests whose Content-Type is application/vnd.saxon+xml.

The content type that Saxon handles is application/vnd.saxon+xml. Base64 encoded files can be embedded in the XML at any level. For flat request bodies with file upload, multipart/form-data is good. For hierarchical request bodies without embedded files, you should stick to application/json.

<map>
  <map name="article">
    <string name="title">Elixir Rocks</string>
    <integer name="author_id">1</integer>
    <timestamp name="published_at">2017-03-29T10:23:35Z</timestamp>
    <boolean name="private">false</boolean>
    <file name="logo" filename="logo.png" content-type="image/png">
      (Base64 encoded file content here)
    </file>
    <list name="sections">
      <map>
        <string name="content">Elixir really rocks.</string>
        <file name="photo" filename="awesome.jpg" content-type="image/jpeg">
          (Base64 encoded file content here)
        </file>
      </map>
      <map>
        <string name="content">Lorem ipsum ...</string>
        <file name="photo" filename="cool.png" content-type="image/png">
          (Base64 encoded file content here)
        </file>
      </map>
    </list>
  </map>
</map>

Note the timestamps in the XML must be in ISO 8601 format with timezone.

The parser parses such XML and yields

%{
  "article" => %{
    "title" => "Elixir Rocks",
    "author_id" => 1,
    "published_at" => %DateTime{year: 2017, month: 3, day: 29, hour: 10, minute: 23, second: 35, time_zone: "Etc/UTC", ...},
    "private" => false,
    "logo" => %Plug.Upload{filename: "logo.png", content_type: "image/png", ...},
    "sections" => [
      %{
        "content" => "Elixir really rocks.",
        "photo" => %Plug.Upload{filename: "awesome.jpg", content_type: "image/jpeg", ...}
      },
      %{
        "content" => "Lorem ipsum ...",
        "photo" => %Plug.Upload{filename: "cool.png", content_type: "image/png"}
      }
    ]
  }
}

Link to this section Summary

Functions

Attempts to parse the connection’s request body given the content-type type, subtype, and its parameters

Link to this section Functions

Link to this function parse(conn, supertype, subtype, headers, opts \\ [])

Attempts to parse the connection’s request body given the content-type type, subtype, and its parameters.

The arguments are:

  • the Plug.Conn connection
  • type, the content-type type (e.g., "x-sample" for the "x-sample/json" content-type)
  • subtype, the content-type subtype (e.g., "json" for the "x-sample/json" content-type)
  • params, the content-type parameters (e.g., %{"foo" => "bar"} for the "text/plain; foo=bar" content-type)

This function should return:

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

Callback implementation for Plug.Parsers.parse/5.