By default, Express 4.x or later doesn’t come with anything to parse request bodies. Therefore, we need to add something to do this.
In this article, we’ll look at how to use the body-parser middleware to do this with text and URL-encoded request bodies.
Parsing Text Bodies
We can parse text request bodies with the text method. It supports automatic inflation of gzip and deflate encodings.
The parsed string will be set as the value of req.body.
It takes an optional option object with the following properties:
defaultCharset— specifies the default character set for the text content if it’s not specified in theContent-Typeheader. Defaults toutf-8.inflate— compressed request bodies will be inflated when this is set totrue. Otherwise, they’ll be rejected.limit— controls the maximum request body size. If it’s number, then it’s measured in bytes. If it’s a string then it can be parsed into a number of bytes.type— this is used to determine what media type it’ll parse. It can be a string, array of strings or a function. If it’s not a function, then it’s directly passed into thetype-islibrary. Otherwise, the request is parsed if the data type the function is called with returns a truthy valueverify— this is a function with signature(req, res, buf, encoding), wherebufis a Buffer object of the raw request body. The parsing can be aborted by throwing an error in the function.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const options = {
inflate: true,
limit: 1000,
defaultCharset: 'utf-8'
};
app.use(bodyParser.text(options));
app.post('/', (req, res) => {
res.send(req.body);
});
app.listen(3000);
Then when we make a POST request to / with the body foo, we get foo back.
Parsing URL Encoded Request Bodies
We can use the urlencoded method to pares URL encoded bodies. It supports automatic inflation of gzip and deflate encodings.
The parsed request body will be set as the value of req.body. The object will contain key-value pairs, where the value can be a string or an array when extended is set to false or anything otherwise.
It takes an optional option object with the following properties:
extended— theextendedoption allows us to choose between parsing the URL-encoded data with thequerystringlibrary whenfalseor theqslibrary when this is set totrue. Theextendedsyntax lets us encode rich objects and arrays, allowing for a JSON-like experience with URL-encoded. The default value istrue, but using the default value has been deprecated.inflate— compressed request bodies will be inflated when this is set totrue. Otherwise, they’ll be rejected.limit— controls the maximum request body size. If it’s number, then it’s measured in bytes. If it’s a string then it can be parsed into a number of bytes.parameterLimit— lets us control the maximum number that are allowed in the URL encoded data. If it’s more than the given value then a 413 response code will be returned. The default is 1000.type— this is used to determine what media type it’ll parse. It can be a string, array of strings or a function. If it’s not a function, then it’s directly passed into thetype-islibrary. Otherwise, the request is parsed if the data type the function is called with returns a truthy valueverify— this is a function with signature(req, res, buf, encoding), wherebufis a Buffer object of the raw request body. The parsing can be aborted by throwing an error in the function.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const options = {
inflate: true,
limit: 1000,
extended: true
};
app.use(bodyParser.urlencoded(options));
app.post('/', (req, res) => {
res.send(req.body);
});
app.listen(3000);
Then when we send a URL encoded POST body to the / route with the body name=Mary&age=10, then we get:
{"name":"Mary","age":"10"}
We can send an array by sending:
name=Mary&age=10&favoriteFood=apple&favoriteFood=orange
Then we get back:
{"name":"Mary","age":"10","favoriteFood":["apple","orange"]}
as the response. This assumes that extends is true.
If we set the parameterLimit to 1 as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const options = {
inflate: true,
limit: 1000,
extended: true,
parameterLimit: 1,
};
app.use(bodyParser.urlencoded(options));
app.post('/', (req, res) => {
res.send(req.body);
});
app.listen(3000);
then we get a 413 error back.
Conclusion
With the text method, body-parser can parse text request bodies. We’ll get a string with the parsed body with req.body.
To parse URL-encoded request bodies, we can use the urlencoded method. It can parse arrays and objects. URL-encoded bodies are sent as query strings, and we can send a query with the same key multiple times to make it parsed as an array.