0
0
NestJSframework~20 mins

Request body in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Request Body Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending a POST request with JSON body?

Consider this NestJS controller method:

  @Post('create')
  create(@Body() data: any) {
    return { received: data.name };
  }

If you send a POST request to /create with JSON body {"name": "Alice"}, what will be the response?

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

@Controller()
export class AppController {
  @Post('create')
  create(@Body() data: any) {
    return { received: data.name };
  }
}
A{"received":null}
B{"received":"Alice"}
C{"received":"{\"name\": \"Alice\"}"}
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Think about how NestJS parses JSON request bodies automatically.

📝 Syntax
intermediate
2:00remaining
Which option correctly extracts nested properties from request body?

Given this NestJS controller method:

  @Post('user')
  createUser(@Body() body: any) {
    const city = ???;
    return city;
  }

The request body JSON is {"user": {"address": {"city": "Paris"}}}. Which code correctly assigns city to "Paris"?

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

@Controller()
export class AppController {
  @Post('user')
  createUser(@Body() body: any) {
    const city = ???;
    return city;
  }
}
Abody.city
Bbody.address.city
Cbody.user.address.city
Dbody.user.city
Attempts:
2 left
💡 Hint

Follow the JSON structure step by step.

🔧 Debug
advanced
2:00remaining
Why does this NestJS controller method fail to parse JSON body?

Look at this controller method:

  @Post('data')
  receiveData(@Body('info') info: string) {
    return info;
  }

A client sends JSON {"user": {"info": "hello"}} but the method returns undefined. Why?

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

@Controller()
export class AppController {
  @Post('data')
  receiveData(@Body('info') info: string) {
    return info;
  }
}
AThe client did not send the 'info' property at the root level, so @Body('info') returns undefined.
BThe @Body('info') decorator extracts the 'info' property, but the client sent a string instead of JSON.
CThe client sent JSON with Content-Type other than application/json, so body parsing failed.
DThe method expects a string but the body parser returns an object, causing undefined.
Attempts:
2 left
💡 Hint

Check the JSON structure and what @Body('info') expects.

state_output
advanced
2:00remaining
What is the output of this controller method with partial body?

Given this method:

  @Post('update')
  update(@Body() body: { id: number; name?: string }) {
    return { id: body.id, name: body.name ?? 'Unknown' };
  }

If the client sends {"id": 5} (no name), what is the response?

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

@Controller()
export class AppController {
  @Post('update')
  update(@Body() body: { id: number; name?: string }) {
    return { id: body.id, name: body.name ?? 'Unknown' };
  }
}
A{"id":5,"name":null}
B400 Bad Request
C{"id":5}
D{"id":5,"name":"Unknown"}
Attempts:
2 left
💡 Hint

Look at the nullish coalescing operator ??.

🧠 Conceptual
expert
2:00remaining
Which statement about NestJS request body parsing is TRUE?

Choose the correct statement about how NestJS handles request bodies.

ANestJS automatically parses JSON bodies only if the Content-Type header is 'application/json'.
BNestJS requires manual parsing of JSON bodies using JSON.parse inside controller methods.
CNestJS ignores the request body if the route method uses @Body() decorator.
DNestJS can parse XML request bodies by default without extra configuration.
Attempts:
2 left
💡 Hint

Think about how HTTP headers affect body parsing.