Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Handling Request Body Data | Working with Data
Building Backend Applications with Nest.js

bookHandling Request Body Data

Desliza para mostrar el menú

Request body data is used to send information from the client to the server. It is commonly used when creating or updating data.

In Nest.js, you can access the request body using the @Body() decorator.

Here is an example:

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

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() body: any) {
    return body;
  }
}

In this example:

  • @Post(): handles POST requests;
  • @Body(): extracts data from the request body;
  • body: contains the data sent by the client.

You can send a request with data like this:

{
  "name": "Alice",
  "age": 30
}

The controller receives this data and can use it inside the method.

Instead of using any, you can type the data:

createUser(@Body() body: { name: string; age: number }) {
  return body;
}

This makes your code more predictable and easier to work with.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 5. Capítulo 1
some-alt