0
0
NestJSframework~10 mins

Request body in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Request body
Client sends HTTP request
NestJS Controller receives request
@Body() decorator extracts body data
Controller method uses extracted data
Response sent back to client
The flow shows how NestJS extracts the request body using @Body() in a controller method to use the data.
Execution Sample
NestJS
import { Controller, Post, Body } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() body: any) {
    return body;
  }
}
This code defines a POST endpoint '/users' that receives JSON data in the request body and returns it.
Execution Table
StepActionInputBody ExtractedOutput
1Client sends POST /users{"name":"Alice"}N/AN/A
2NestJS receives requestPOST /users with bodyN/AN/A
3@Body() extracts body{"name":"Alice"}{"name":"Alice"}N/A
4Controller method runs{"name":"Alice"}{"name":"Alice"}{"name":"Alice"}
5Response sentN/AN/A{"name":"Alice"}
💡 Request handled and response sent with extracted body data.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
bodyundefined{"name":"Alice"}{"name":"Alice"}{"name":"Alice"}
Key Moments - 2 Insights
Why do we use @Body() in the controller method?
The @Body() decorator tells NestJS to extract the JSON data sent in the request body so the method can use it, as shown in step 3 of the execution_table.
What happens if the client sends no body?
Then @Body() extracts undefined or an empty object, so the variable 'body' will be empty, which you can check before using it (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'body' after step 3?
Aundefined
B{"name":"Alice"}
Cnull
D{}
💡 Hint
Check the 'Body Extracted' column at step 3 in the execution_table.
At which step does the controller method return the extracted body?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table to see when the body is returned.
If the client sends an empty body, how would the 'body' variable change in variable_tracker?
AIt would cause an error and stop execution
BIt would be the same as before step 3
CIt would be undefined or empty object after step 3
DIt would contain previous request data
💡 Hint
Refer to key_moments about empty body and variable_tracker initial values.
Concept Snapshot
NestJS Request Body Handling:
- Use @Body() decorator in controller method parameter.
- Extracts JSON data sent by client in request body.
- Works with POST, PUT, PATCH requests.
- Access extracted data as method argument.
- Return or process data as needed.
Full Transcript
In NestJS, when a client sends an HTTP request with a body, such as JSON data, the controller can access this data using the @Body() decorator. The flow starts with the client sending a POST request to an endpoint. NestJS receives the request and uses @Body() to extract the body content. The controller method then receives this extracted data as a parameter and can use it, for example, to return it back or save it. If the client sends no body, the extracted data will be undefined or empty. This process allows easy handling of request data in NestJS controllers.