Get HTTP POST Body in Express.js

Spread the love

In this brief article we’ll be going over how to extract information from a POST body in Express.js. The HTTP protocol provides a number of ways to pass information from a client to a server, with POST bodies being the most flexible and most commonly used method to send data via HTTP.
Another way, which is typically used for different use-cases, is to convey information using query strings or URL parameters. For more info, check out how to get query strings and parameters in Express.js.
For this article I assume you have some experience with Node.js and creating simple Express.js servers.
Sending POST Data in HTTP
Data can be sent via an HTTP POST call for many reasons, with some of the most common being via an HTML or an API request. The data can take on a few different forms, with the most common being:
application/x-www-form-urlencoded: Data in this encoding is formatted like a query string you’d see in a URL, with key-value paris being separated by & characters. For example: foo=bar&abc=123&stack=abuse. This is the default encoding.
multipart/form-data: This encoding is typically used for sending files. In short, each key-value is sent in the same request, but different “parts”, which are separated by “boundaries” and include more meta-data.
text/plain: This data is just sent as unstructured plain text, and typically is not used.
A raw HTTP POST request with the application/x-www-form-urlencoded encoding might look something like this:
POST /signup HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 53


Extracting POST Data
Before we can get started accessing POST data right away, we need to properly configure our Express app. This configuration needs to be done since not all web app servers need body parsing, and what body parsing is enabled depends on your application.
To set this up, we’ll be using the body-parser package, which can handle many forms of data. This package is a middleware that intercepts the raw body and parses it in to a form that your application code can easily use.
// index.js
const express = require(‘express’);
const bodyParser = require(‘body-parser’);

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));‘/post-test’, (req, res) => {
console.log(‘Got body:’, req.body);

app.listen(8080, () => console.log(`Started server at http://localhost:8080!`));

Notice how we call app.use(…) before defining our route. The order here matters. This will ensure that the body-parser will run before our route, which ensures that our route can then access the parsed HTTP POST body.
To test this, we’ll first start the Express app and then use the curl utility in a different console window:
$ curl -d “username=scott&password=secret&” -X POST http://localhost:8080/post-test

$ node index.js
Started server at http://localhost:8080!
Got body: { username: ‘scott’,
password: ‘secret’,
website: ‘’ }

Here you can see that the query string data was parsed in to a JavaScript object that we can easily access.
Another important thing to note is our use of the extended option when calling bodyParser.urlencoded. Using the extended option tells body-parser to use the qs library to parse the URL-encoded data. This allows for things like objects and arrays to be encoded into the URL-encoded format.
And while urlencoded is one of the most commonly used parsers that body-parser provides, you can also use the following:
.json(): Parses JSON-formatted text for bodies with a Content-Type of application/json.
.raw(): Parses HTTP body in to a Buffer for specified custom Content-Types, although the default accepted Content-Type is application/octet-stream.
.text(): Parses HTTP bodies with a Content-Type of text/plain, which returns it as a plain string.
Each of these parsers also supports automatic inflation of data encoded with gzip or deflate, so compression can still be transparently used without any extra work by your application code.
The great thing about the middleware model and how this package parses data is that you’re not stuck to using just one parser. You can enable one or more parsers for your app to ensure that all data types are processed properly:
// index.js
const express = require(‘express’);
const bodyParser = require(‘body-parser’);

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

// …

So now if we were to send an HTTP POST request with JSON as the body, it will be parsed in to a regular JSON object in the req.body property:
POST /post-test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 69


$ node index.js
Started server at http://localhost:8080!
Got body: { username: ‘scott’,
password: ‘secret’,
website: ‘’ }

The most common way to send diverse and large amounts of data via HTTP is to use the POST method. Before we can easily access this data on the server side in Express, we need to use some middleware, like the body-parser package, to parse the data in to a format that we can easily access. Once the data from the raw HTTP request is parsed, it can then be accessed via the body property of the req object.
In this article we explained a bit about the various content types/encodings, how to use the body-parser package and its various options, and how to access the parsed data. If you see any issues or have suggestions, feel free to leave a comment below.

X ITM Cloud News


Next Post

Deploying a Node.js App to a DigitalOcean Droplet with Docker

Sun Nov 24 , 2019
Spread the love          Introduction JavaScript has come a long way over the years, and we’re now at a point where you can write and deploy a web application very easily. Frameworks like Express, Sails, and Meteor have only made this easier. Following most tutorials on the internet means you’ll be working […]

Cloud Computing – Consultancy – Development – Hosting – APIs – Legacy Systems

X-ITM Technology helps our customers across the entire enterprise technology stack with differentiated industry solutions. We modernize IT, optimize data architectures, and make everything secure, scalable and orchestrated across public, private and hybrid clouds.

This image has an empty alt attribute; its file name is x-itmdc.jpg

The enterprise technology stack includes ITO; Cloud and Security Services; Applications and Industry IP; Data, Analytics and Engineering Services; and Advisory.

Watch an animation of  X-ITM‘s Enterprise Technology Stack

We combine years of experience running mission-critical systems with the latest digital innovations to deliver better business outcomes and new levels of performance, competitiveness and experiences for our customers and their stakeholders.

X-ITM invests in three key drivers of growth: People, Customers and Operational Execution.

The company’s global scale, talent and innovation platforms serve 6,000 private and public-sector clients in 70 countries.

X-ITM’s extensive partner network helps drive collaboration and leverage technology independence. The company has established more than 200 industry-leading global Partner Network relationships, including 15 strategic partners: Amazon Web Services, AT&T, Dell Technologies, Google Cloud, HCL, HP, HPE, IBM, Micro Focus, Microsoft, Oracle, PwC, SAP, ServiceNow and VMware