Explicit Body

By default, Elysia will try to determine body parsing function ahead of time and pick the most suitable function to speed up the process.

Elysia is able to determine that body function by reading body.

Take a look at this example:

app.post("/", ({ body }) => body, {
  body: t.Object({
    username: t.String(),
    password: t.String(),
  }),
});

Elysia read the body schema and found that, the type is entirely an object, so it's likely that the body will be JSON. Elysia then picks the JSON body parser function ahead of time and tries to parse the body.

Here's a criteria that Elysia uses to pick up type of body parser

  • application/json: body typed as t.Object
  • multipart/form-data: body typed as t.Object, and is 1 level deep with t.File
  • application/x-www-form-urlencoded: body typed as t.URLEncoded
  • text/plain: other primitive type

This allows Elysia to optimize body parser ahead of time, and reduce overhead in compile time.

Explicit Content Type

However, in some scenario if Elysia fails to pick the correct body parser function, you can explicitly tell Elysia to use a certain function by specifying type

app.post("/", ({ body }) => body, {
  // Short form of application/json
  type: "json",
});

This allows you to control Elysia behavior for picking body parser function to fit your needs in a complex scenario.

type may be one of the following:

type ContentType = |
    // Shorthand for 'text/plain'
    | 'text'
    // Shorthand for 'application/json'
    | 'json'
    // Shorthand for 'multipart/form-data'
    | 'formdata'
    // Shorthand for 'application/x-www-form-urlencoded'\
    | 'urlencoded'
    | 'text/plain'
    | 'application/json'
    | 'multipart/form-data'
    | 'application/x-www-form-urlencoded'

You can extend Elysia by adding a custom body parser function yourself with onParse, see body parser.