0
0
NestJSframework~10 mins

Request body in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the request body in a NestJS controller method.

NestJS
import { Controller, Post, [1] } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Post()
  createUser(@[1]() body: any) {
    return body;
  }
}
Drag options to blanks, or click blank then click option'
AQuery
BParam
CBody
DHeaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Param() instead of @Body() to get the request body.
Forgetting to import the correct decorator.
Using @Query() which extracts query parameters, not the body.
2fill in blank
medium

Complete the code to define a DTO class and use it to type the request body in a NestJS controller.

NestJS
import { Controller, Post, Body } from '@nestjs/common';

class CreateUserDto {
  name: string;
  age: number;
}

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() body: [1]) {
    return body;
  }
}
Drag options to blanks, or click blank then click option'
AUser
Bany
CObject
DCreateUserDto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' defeats the purpose of typing the request body.
Using a class name that is not defined.
Using 'Object' which is less specific than the DTO.
3fill in blank
hard

Fix the error in the code to correctly extract the request body in a NestJS controller method.

NestJS
import { Controller, Post, Body } from '@nestjs/common';

@Controller('products')
export class ProductsController {
  @Post()
  addProduct(@[1] body: any) {
    return body;
  }
}
Drag options to blanks, or click blank then click option'
ABody()
BParam()
CQuery()
DRequest()
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Body without parentheses.
Using other decorators like @Param or @Query incorrectly.
Forgetting to import the decorator.
4fill in blank
hard

Fill both blanks to create a POST route that extracts and types the request body using a DTO in NestJS.

NestJS
import { Controller, Post, [1] } from '@nestjs/common';

class UpdateUserDto {
  email: string;
  password: string;
}

@Controller('users')
export class UsersController {
  @Post('update')
  updateUser(@[2]() body: UpdateUserDto) {
    return body;
  }
}
Drag options to blanks, or click blank then click option'
ABody
BParam
CQuery
DHeaders
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong decorator.
Using different decorators for import and parameter.
Forgetting parentheses in the parameter decorator.
5fill in blank
hard

Fill all three blanks to create a NestJS controller method that extracts a typed request body and returns a confirmation message.

NestJS
import { Controller, Post, [1] } from '@nestjs/common';

class CreateOrderDto {
  productId: number;
  quantity: number;
}

@Controller('orders')
export class OrdersController {
  @Post()
  createOrder(@[2]() order: [3]) {
    return `Order for product ${order.productId} with quantity ${order.quantity} received.`;
  }
}
Drag options to blanks, or click blank then click option'
ABody
BCreateOrderDto
COrderDto
DParam
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong DTO class name.
Using @Param instead of @Body.
Not importing the @Body decorator.