2018-10-12 15:37:13 -04:00
---
title: Use body-parser to Parse POST Requests
---
2019-07-24 00:59:27 -07:00
# Use body-parser to Parse POST Requests
2019-07-02 07:05:15 +05:30
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
The body-parser should already be added to your project if you used the provided boilerplate, but if not it should be there as:
2019-06-13 06:09:52 +05:30
```json
2018-10-12 15:37:13 -04:00
"dependencies": {
2019-07-02 07:05:15 +05:30
"body-parser": "^1.19.0",
2019-07-24 00:59:27 -07:00
2019-07-02 07:05:15 +05:30
"express": "^4.17.1"
}
2018-10-12 15:37:13 -04:00
```
2019-07-02 07:05:15 +05:30
You can run `npm install body-parser` to add it as a dependency to your project instead of manually adding it to the `package.json` file.
This guide assumes you have imported the `body-parser` module into your file as `bodyParser` .
In order to import the same, you just need to add the following line at the top of your file:
```javascript
2019-07-24 00:59:27 -07:00
var bodyParser = require("body-parser");
2019-07-02 07:05:15 +05:30
```
All you need to do for this challenge is pass the middleware to `app.use()` . Make sure it comes before the paths it needs to be used on. Remember that body-parser returns with `bodyParser.urlencoded({extended: false})` . Use the following as a template:
2018-10-21 18:24:20 +03:00
```javascript
2019-06-13 06:09:52 +05:30
app.use(bodyParser.urlencoded({ extended: false }));
2019-07-02 07:05:15 +05:30
```
In order to parse JSON data sent in the POST request, use `bodyParser.json()` as the middleware as shown below:
2019-06-13 06:09:52 +05:30
2019-07-02 07:05:15 +05:30
```javascript
app.use(bodyParser.json());
2018-10-21 18:24:20 +03:00
```
2018-10-12 15:37:13 -04:00
2019-07-02 07:05:15 +05:30
The data received in the request is available in the `req.body` object.
Do not forget that all these statements need to go above any routes that might have been defined.