Middleware for parsing URL-encoded form data. Only parses requests where the Content-Type header matches the type option and supports automatic inflation of compressed bodies.
Creates middleware that parses URL-encoded bodies and populates req.body with the parsed data as key-value pairs.
/**
* Create middleware to parse URL-encoded bodies
* @param {object} [options] - Configuration options
* @returns {function} Express middleware function
*/
function urlencoded(options?: {
extended?: boolean;
inflate?: boolean;
limit?: number | string;
parameterLimit?: number;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
defaultCharset?: string;
charsetSentinel?: boolean;
interpretNumericEntities?: boolean;
depth?: number;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;booleanfalsefalse, uses querystring library (strings/arrays only). When true, uses qs library allowing rich objects and arraysbooleantruetrue, deflated (compressed) bodies will be inflated; when false, deflated bodies are rejectednumber | string'100kb'number1000string | string[] | function'application/x-www-form-urlencoded'functionundefinedverify(req, res, buf, encoding) where buf is the raw request body Buffer. Can abort parsing by throwing an errorstring'utf-8'booleanfalseutf8 parameter take precedence as charset selectorbooleanfalse☺ when parsing iso-8859-1 formsnumber32extended is true. Limits the amount of keys parsed to prevent abuseBasic URL-encoded parsing (simple mode):
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
// Simple parsing - strings and arrays only
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/form', (req, res) => {
console.log(req.body); // { name: "John", age: "30", hobbies: ["reading", "coding"] }
res.send('Form received');
});Extended parsing for rich objects:
// Extended parsing - supports nested objects
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/form', (req, res) => {
console.log(req.body);
// { user: { name: "John", profile: { age: 30, active: true } } }
res.send('Form received');
});Custom options:
app.use(bodyParser.urlencoded({
extended: true,
limit: '10mb',
parameterLimit: 5000,
depth: 10,
defaultCharset: 'iso-8859-1'
}));Charset handling:
app.use(bodyParser.urlencoded({
extended: false,
charsetSentinel: true,
interpretNumericEntities: true,
defaultCharset: 'iso-8859-1'
}));Custom type checking:
app.use(bodyParser.urlencoded({
extended: true,
type: (req) => {
return req.headers['content-type'] &&
req.headers['content-type'].includes('form-urlencoded');
}
}));With verification:
app.use(bodyParser.urlencoded({
extended: false,
verify: (req, res, buf, encoding) => {
// Ensure form data isn't too complex
const paramCount = buf.toString().split('&').length;
if (paramCount > 100) {
throw new Error('Too many form parameters');
}
}
}));querystring libraryname=John&age=30&hobbies=reading&hobbies=codingqs library for rich parsinguser[profile][name]=John&user[profile][age]=30The URL-encoded parser can throw several types of errors:
app.use(bodyParser.urlencoded({ extended: false }));
app.use((err, req, res, next) => {
if (err.type === 'parameters.too.many') {
return res.status(413).send('Too many form parameters');
}
if (err.type === 'querystring.parse.rangeError') {
return res.status(400).send('Form data too deep');
}
if (err.type === 'charset.unsupported') {
return res.status(415).send('Unsupported charset');
}
next(err);
});