0
0
Node.jsframework~10 mins

Parsing request body (JSON and form data) in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parsing request body (JSON and form data)
Client sends HTTP request
Server receives request
Check Content-Type header
Parse JSON body
Store parsed data in req.body
Use req.body in route handler
Send response
The server checks the request's Content-Type to decide how to parse the body, then stores the parsed data for use in the route.
Execution Sample
Node.js
import express from 'express';
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post('/submit', (req, res) => {
  res.json(req.body);
});
This code sets up an Express server that parses JSON and form data bodies, then echoes back the parsed data.
Execution Table
StepActionContent-TypeParsing Methodreq.body ContentResponse Sent
1Receive POST /submitapplication/jsonCheck Content-Type{}No
2Parse bodyapplication/jsonexpress.json() middleware{"name":"Alice"}No
3Route handler runsapplication/jsonUse req.body{"name":"Alice"}No
4Send responseapplication/jsonSend JSON response{"name":"Alice"}Yes
5Receive POST /submitapplication/x-www-form-urlencodedCheck Content-Type{}No
6Parse bodyapplication/x-www-form-urlencodedexpress.urlencoded() middleware{"name":"Bob","age":"30"}No
7Route handler runsapplication/x-www-form-urlencodedUse req.body{"name":"Bob","age":"30"}No
8Send responseapplication/x-www-form-urlencodedSend JSON response{"name":"Bob","age":"30"}Yes
9Receive POST /submittext/plainCheck Content-Type{}No
10Parse bodytext/plainNo parser matches{}No
11Route handler runstext/plainreq.body empty{}No
12Send responsetext/plainSend JSON response{}Yes
💡 Parsing stops after sending response or when no matching parser is found.
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 10Final
req.body{}{"name":"Alice"}{"name":"Bob","age":"30"}{}{}
Key Moments - 3 Insights
Why is req.body empty when Content-Type is text/plain?
Because express.json() and express.urlencoded() only parse JSON and form data. No parser runs for text/plain, so req.body stays empty as shown in steps 9-11.
How does the server know which parser to use?
The server checks the Content-Type header (step 1 and 5). If it is application/json, express.json() parses the body. If it is application/x-www-form-urlencoded, express.urlencoded() parses it.
Why is age a string in req.body after form data parsing?
Form data values are always strings by default, so age is "30" as a string, not a number, as seen in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is req.body content after step 6?
A{"name":"Bob","age":"30"}
B{"name":"Alice"}
C{}
Dnull
💡 Hint
Check the 'req.body Content' column at step 6 in the execution_table.
At which step does the server send the response for a JSON request?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look for 'Response Sent' marked 'Yes' for application/json in the execution_table.
If the Content-Type was missing, what would req.body be?
Anull
B{"name":"Alice"}
C{}
Dundefined
💡 Hint
Refer to steps where no matching parser runs and req.body remains empty.
Concept Snapshot
Parsing request body in Node.js with Express:
- Use express.json() to parse JSON bodies.
- Use express.urlencoded({ extended: true }) to parse form data.
- Server checks Content-Type header to pick parser.
- Parsed data is stored in req.body.
- If Content-Type is unsupported, req.body is empty.
- Use req.body in route handlers to access client data.
Full Transcript
When a client sends a POST request, the server first checks the Content-Type header to decide how to parse the body. If the Content-Type is application/json, the express.json() middleware parses the JSON string into a JavaScript object and stores it in req.body. If the Content-Type is application/x-www-form-urlencoded, the express.urlencoded() middleware parses the form data string into an object with string values and stores it in req.body. If the Content-Type is unsupported, no parser runs and req.body remains empty. The route handler then uses req.body to access the parsed data and sends it back as a JSON response. This process ensures the server understands the client's data format and can work with it easily.