Bird
Raised Fist0
NestJSframework~20 mins

Request body in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. In NestJS, which decorator is used to access the data sent in the request body?
easy
A. @Body()
B. @Param()
C. @Query()
D. @Headers()

Solution

  1. Step 1: Understand the purpose of decorators in NestJS

    Decorators like @Body(), @Param(), @Query(), and @Headers() extract different parts of the request.
  2. Step 2: Identify which decorator accesses the request body

    @Body() specifically extracts the data sent in the request body from the client.
  3. Final Answer:

    @Body() -> Option A
  4. Quick Check:

    Request body data = @Body() [OK]
Hint: Request body data always uses @Body() decorator [OK]
Common Mistakes:
  • Using @Param() to get body data
  • Confusing @Query() with @Body()
  • Trying to access body data without any decorator
2. Which of the following is the correct way to type the request body in a NestJS controller method?
easy
A. create(@Body: any data) {}
B. create(@Body data: any) {}
C. create(@Body() data: any) {}
D. create(@Body data) {}

Solution

  1. Step 1: Recall the syntax for decorators with typed parameters

    In NestJS, the decorator is followed by parentheses and the parameter is typed after the colon.
  2. Step 2: Check each option's syntax

    create(@Body() data: any) {} correctly uses @Body() with typed parameter 'data: any'. Options B, C, and D have syntax errors or missing parentheses.
  3. Final Answer:

    create(@Body() data: any) {} -> Option C
  4. Quick Check:

    Correct decorator syntax = create(@Body() data: any) {} [OK]
Hint: Always use parentheses with @Body() and type after colon [OK]
Common Mistakes:
  • Omitting parentheses after @Body
  • Placing type before parameter name
  • Not typing the parameter at all
3. Given this NestJS controller method, what will be the output if the client sends {"name": "Alice"} in the request body?
  @Post()
  create(@Body() data: { name: string }) {
    return `Hello, ${data.name}!`;
  }
medium
A. "Hello, null!"
B. "Hello, undefined!"
C. Error: data.name is not defined
D. "Hello, Alice!"

Solution

  1. Step 1: Understand the method behavior with typed body

    The method expects an object with a 'name' string property and returns a greeting using that name.
  2. Step 2: Analyze the input and output

    The client sends {"name": "Alice"}, so data.name is "Alice". The method returns "Hello, Alice!".
  3. Final Answer:

    "Hello, Alice!" -> Option D
  4. Quick Check:

    Input name "Alice" produces greeting "Hello, Alice!" [OK]
Hint: Match input keys to typed properties for correct output [OK]
Common Mistakes:
  • Assuming undefined if typing is missing
  • Expecting runtime error for missing type
  • Confusing property names in the body
4. Identify the error in this NestJS controller method:
  @Post()
  create(@Body data: { age: number }) {
    return `Age is ${data.age}`;
  }
medium
A. Return statement syntax error
B. Missing parentheses after @Body decorator
C. Incorrect type for data parameter
D. Method should be named differently

Solution

  1. Step 1: Check the decorator syntax

    @Body decorator requires parentheses even if empty, so it should be @Body()
  2. Step 2: Verify other parts of the method

    The type and return statement are correct; only the decorator syntax is wrong.
  3. Final Answer:

    Missing parentheses after @Body decorator -> Option B
  4. Quick Check:

    @Body() needs parentheses [OK]
Hint: Always write @Body() with parentheses, even if empty [OK]
Common Mistakes:
  • Omitting parentheses after @Body
  • Misplacing type annotation
  • Assuming method name causes error
5. You want to accept a request body with optional fields in NestJS. Which method signature correctly types the body to allow optional 'email' and required 'username' fields?
  @Post()
  create(???) {
    // method body
  }
hard
A. @Body() data: { username: string; email?: string }
B. @Body() data: { username?: string; email: string }
C. @Body() data: { username: string; email: string }
D. @Body() data: { username?: string; email?: string }

Solution

  1. Step 1: Understand optional and required fields in TypeScript types

    A question mark (?) after a property name means it is optional; absence means required.
  2. Step 2: Match the requirement to the type

    Username is required, so no '?'. Email is optional, so it has '?'. @Body() data: { username: string; email?: string } matches this.
  3. Final Answer:

    @Body() data: { username: string; email?: string } -> Option A
  4. Quick Check:

    Required username, optional email = @Body() data: { username: string; email?: string } [OK]
Hint: Use '?' after optional fields in type definitions [OK]
Common Mistakes:
  • Marking required fields as optional
  • Marking optional fields as required
  • Forgetting to use @Body() decorator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard