Body Parser
This guide covers the body parser configuration in AdonisJS. You will learn how to:
- Configure parsers for different content types (JSON, form data, multipart)
- Set global parsing options like empty string conversion and whitespace trimming
- Adjust file upload limits and request size restrictions
- Control automatic file processing for specific routes
- Handle custom content types using the raw parser
Overview
The body parser is responsible for parsing incoming request bodies before they reach your route handlers. It automatically detects the content type of each request and applies the appropriate parser to convert the raw request data into a usable format.
AdonisJS includes three built-in parsers: the JSON parser handles JSON-encoded data, the form parser handles URL-encoded form submissions, and the multipart parser handles file uploads and multipart form data. Each parser can be configured independently through the config/bodyparser.ts file.
You don't interact with the body parser directly in your application code. Instead, you access the parsed data through the Request class using methods like request.all(), request.body(), or request.file(). The body parser runs as middleware and processes request bodies automatically before your route handlers execute.
See also: Request class documentation for accessing parsed request data.
Configuration
The body parser is configured in the config/bodyparser.ts file. The configuration file is created automatically when you create a new AdonisJS application.
import { defineConfig } from '@adonisjs/core/bodyparser'
const bodyParserConfig = defineConfig({
allowedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
form: {
convertEmptyStringsToNull: true,
trimWhitespaces: true,
types: ['application/x-www-form-urlencoded'],
},
json: {
convertEmptyStringsToNull: true,
trimWhitespaces: true,
types: [
'application/json',
'application/json-patch+json',
'application/vnd.api+json',
'application/csp-report',
],
},
multipart: {
autoProcess: true,
convertEmptyStringsToNull: true,
trimWhitespaces: true,
processManually: [],
limit: '20mb',
types: ['multipart/form-data'],
},
})
export default bodyParserConfig
allowedMethods
The allowedMethods array defines which HTTP methods should have their request bodies parsed. By default, only POST, PUT, PATCH, and DELETE requests are processed. GET requests are excluded because they typically don't include request bodies.
Global parsing options
Two global options are available across all parsers: convertEmptyStringsToNull and trimWhitespaces. These options help normalize incoming data before it reaches your application logic.
convertEmptyStringsToNull
The convertEmptyStringsToNull option converts all empty strings in the request body to null values. This option solves a common problem with HTML forms.
When an HTML form input field has no value, browsers send an empty string in the request body rather than omitting the field entirely. This behavior creates challenges for database normalization, especially with nullable columns.
Consider a user registration form with an optional "country" field. Your database has a nullable country column, and you want to store null when the user doesn't select a country. However, the HTML form sends an empty string, which means you would insert an empty string into the database instead of leaving the column as null.
Enabling convertEmptyStringsToNull handles this inconsistency automatically. The body parser converts all empty strings to null before your validation or database logic runs.
json: {
convertEmptyStringsToNull: true,
}
trimWhitespaces
The trimWhitespaces option removes leading and trailing whitespace from all string values in the request body. This helps eliminate accidental whitespace that users might include when submitting forms.
Instead of manually trimming values in your controllers or validators, you can enable this option and let the body parser handle whitespace removal globally.
form: {
trimWhitespaces: true,
}
JSON parser
The JSON parser handles requests with JSON-encoded bodies. It processes several content types by default, including application/json, application/json-patch+json, application/vnd.api+json, and application/csp-report.
encoding
The encoding option specifies the character encoding to use when converting the request body Buffer to a string. The default is utf-8, which handles most use cases. You can use any encoding supported by the
iconv-lite package.
json: {
encoding: 'utf-8',
}
limit
The limit option sets the maximum size of request body data the parser will accept. Requests that exceed this limit will receive a 413 Payload Too Large error response.
json: {
limit: '1mb',
}
strict
The strict option controls whether the parser accepts only objects and arrays as top-level JSON values. When enabled, the parser rejects primitive values like strings, numbers, or booleans at the root level.
json: {
strict: true,
}
types
The types array defines which content types the JSON parser should handle. You can add custom content types if your application receives JSON data with non-standard content type headers.
json: {
types: [
'application/json',
'application/json-patch+json',
'application/vnd.api+json',
'application/csp-report',
'application/custom+json',
]
}
Form parser
The form parser handles URL-encoded form data, typically from HTML forms with application/x-www-form-urlencoded content type.
encoding
The encoding option specifies the character encoding to use when converting the request body Buffer to a string. The default is utf-8, which handles most use cases. You can use any encoding supported by the
iconv-lite package.
form: {
encoding: 'utf-8',
}
limit
The limit option sets the maximum size of request body data the parser will accept. Requests that exceed this limit will receive a 413 Payload Too Large error response.
form: {
limit: '1mb',
}
queryString
The queryString option allows you to configure how the URL-encoded string is parsed into an object. These options are passed directly to the
qs package, which handles the parsing.
form: {
queryString: {
depth: 5,
parameterLimit: 1000,
},
}
See also: qs documentation for all available options.
types
The types array defines which content types the form parser should handle. By default, it processes application/x-www-form-urlencoded requests.
form: {
types: ['application/x-www-form-urlencoded'],
}
Multipart parser
The multipart parser handles file uploads and multipart form data. It processes requests with the multipart/form-data content type, which browsers use when submitting forms that include file inputs.
autoProcess
The autoProcess option controls whether uploaded files are automatically moved to your operating system's temporary directory. When enabled, the parser streams files to disk as the request is processed.
After automatic processing, you can access uploaded files in your controllers using request.file(), validate them, and move them to a permanent location or cloud storage service.
multipart: {
autoProcess: true,
}
You can specify an array of route patterns to enable automatic processing for specific routes only. The values must be route patterns, not URLs.
multipart: {
autoProcess: [
'/uploads',
'/posts/:id/images',
],
}
processManually
The processManually array lets you disable automatic file processing for selected routes while keeping it enabled globally. This is useful when you have a few routes that need custom file handling but want the convenience of automatic processing everywhere else.
The values must be route patterns, not URLs.
multipart: {
autoProcess: true,
processManually: [
'/file-manager',
'/projects/:id/assets',
],
}
encoding
The encoding option specifies the character encoding to use when converting text fields in the multipart request to strings. The default is utf-8, which handles most use cases. You can use any encoding supported by the
iconv-lite package.
multipart: {
encoding: 'utf-8',
}
limit
The limit option sets the maximum total size of all uploaded files in a single request. Requests that exceed this limit will receive a 413 Payload Too Large error response.
multipart: {
limit: '20mb',
}
fieldsLimit
The fieldsLimit option sets the maximum total size of all form fields (not files) in the multipart request. This prevents abuse through extremely large text field submissions. Requests that exceed this limit will receive a 413 Payload Too Large error response.
multipart: {
fieldsLimit: '2mb',
}
tmpFileName
The tmpFileName option accepts a function that generates custom names for temporary files. By default, the parser generates random file names.
multipart: {
tmpFileName: () => {
return `upload_${Date.now()}_${Math.random().toString(36)}`
},
}
types
The types array defines which content types the multipart parser should handle. By default, it processes multipart/form-data requests.
multipart: {
types: ['multipart/form-data'],
}
Raw parser for custom content types
The body parser includes a raw parser that can handle content types not supported by the default parsers. The raw parser provides the request body as a string, which you can then process using custom middleware.
This is useful when your application receives data in formats like XML, YAML, or other specialized content types that don't have built-in parsers.
const bodyParserConfig = defineConfig({
allowedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
raw: {
types: ['application/xml', 'text/xml'],
limit: '1mb',
encoding: 'utf-8',
},
form: {
// ... form config
},
json: {
// ... json config
},
multipart: {
// ... multipart config
},
})
export default bodyParserConfig
After enabling the raw parser for specific content types, create custom middleware to parse the string data into a usable format.
See also: Middleware documentation
import type { HttpContext } from '@adonisjs/core/http'
import type { NextFn } from '@adonisjs/core/types/http'
import xml2js from 'xml2js'
export default class XmlParserMiddleware {
async handle({ request }: HttpContext, next: NextFn) {
if (request.header('content-type')?.includes('xml')) {
const rawBody = request.raw()
const parser = new xml2js.Parser()
const parsed = await parser.parseStringPromise(rawBody)
request.updateBody(parsed)
}
await next()
}
}