freeCodeCamp/curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-po...

62 lines
2.7 KiB
Markdown
Raw Normal View History

2018-10-25 18:29:56 +00:00
---
id: 587d7fb2367417b2b2512bf7
title: Use body-parser to Parse POST Requests
challengeType: 2
forumTopicId: 301520
dashedName: use-body-parser-to-parse-post-requests
2018-10-25 18:29:56 +00:00
---
# --description--
fix/reformat-basic-node-express-challenges (#35424) * fix/reformat-basic-node-express-challenges * add semi-colon Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/get-route-parameter-input-from-the-client.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/serve-static-assets.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/start-a-working-express-server.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/05-apis-and-microservices/basic-node-and-express/use-body-parser-to-parse-post-requests.english.md Co-Authored-By: moT01 <20648924+moT01@users.noreply.github.com>
2019-03-06 23:20:17 +00:00
Besides GET, there is another common HTTP verb, it is POST. POST is the default method used to send client data with HTML forms. In REST convention, POST is used to send data to create new items in the database (a new user, or a new blog post). You dont have a database in this project, but you are going to learn how to handle POST requests anyway.
2020-07-17 16:06:24 +00:00
In these kind of requests, the data doesnt appear in the URL, it is hidden in the request body. The body is a part of the HTTP request, also called the payload. Even though the data is not visible in the URL, this does not mean that it is private. To see why, look at the raw content of an HTTP POST request:
```http
POST /path/subpath HTTP/1.0
From: john@example.com
User-Agent: someBrowser/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 20
name=John+Doe&age=25
```
As you can see, the body is encoded like the query string. This is the default format used by HTML forms. With Ajax, you can also use JSON to handle data having a more complex structure. There is also another type of encoding: multipart/form-data. This one is used to upload binary files. In this exercise, you will use a urlencoded body. To parse the data coming from POST requests, you have to install the `body-parser` package. This package allows you to use a series of middleware, which can decode data in different formats.
2018-10-25 18:29:56 +00:00
# --instructions--
2018-10-25 18:29:56 +00:00
Install the `body-parser` module in your `package.json`. Then, `require` it at the top of the file. Store it in a variable named `bodyParser`. The middleware to handle urlencoded data is returned by `bodyParser.urlencoded({extended: false})`. Pass to `app.use()` the function returned by the previous method call. As usual, the middleware must be mounted before all the routes which need it.
2018-10-25 18:29:56 +00:00
**Note:** `extended=false` is a configuration option that tells the parser to use the classic encoding. When using it, values can be only strings or arrays. The extended version allows more data flexibility, but it is outmatched by JSON.
2018-10-25 18:29:56 +00:00
# --hints--
2018-10-25 18:29:56 +00:00
The 'body-parser' middleware should be mounted
2018-10-25 18:29:56 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/add-body-parser').then(
(data) => {
assert.isAbove(
data.mountedAt,
0,
'"body-parser" is not mounted correctly'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2018-10-25 18:29:56 +00:00
# --solutions--
2018-10-25 18:29:56 +00:00
```js
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
2018-10-25 18:29:56 +00:00
```