nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

At controller - HTTP Post with FormData: Empty object for Both CrudRequest, and Entity object #751

Open datngoNFQ opened 2 years ago

datngoNFQ commented 2 years ago

I'm able to use Postman to save data to DB by JSON. But when using FormData, at Controller, I have Empty object for CrudRequest, and the Entity Object

Follow another issue at: https://github.com/nestjsx/crud/issues/541 . Then, within my controller, I implemented code as below:

get base(): CrudController<FunctionScript> {
    return this;
  }
@Override()
  createOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: MyEntity) {
    console.log(req);
    console.log(dto);
    this.base.createOneBase(req, dto);
}

Both req, and dto are empty.

Below are the dependencies that I use for my nestjs project

  "dependencies": {
    "@dvelop-sdk/app-router": "^2.0.4",
    "@dvelop-sdk/identityprovider": "^2.1.7",
    "@nestjs/common": "^8.0.0",
    "@nestjs/config": "^1.1.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/platform-express": "^8.0.0",
    "@nestjs/serve-static": "^2.2.2",
    "@nestjs/typeorm": "^8.0.2",
    "@nestjsx/crud": "^5.0.0-alpha.3",
    "@nestjsx/crud-typeorm": "^5.0.0-alpha.3",
    "aws-sdk": "^2.1029.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "cookie-parser": "^1.4.5",
    "mysql2": "^2.3.3",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "typeorm": "^0.2.40"
  },
datngoNFQ commented 2 years ago

For more information, after installed and use CRUD of nestjsx libraries, event a simple controller as below would receive empty object from HTTP POST from Postman

import { Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';
import { ObjectDto } from './dto/Object.dto';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Post('subscribe')
  subscribe(@Body() objectDto: ObjectDto) {
    console.log(objectDto);
    return '';
  }
}

below is my DTO:

import { IsNotEmpty, IsDefined } from 'class-validator';

export class TenantDto {
  @IsDefined()
  @IsNotEmpty()
  tenantName: string;

  @IsDefined()
  @IsNotEmpty()
  tenantId: string;
}
import { IsNotEmpty, IsDefined } from 'class-validator';

export class ObjectDto {
  @IsDefined()
  @IsNotEmpty()
  objectName: string;

  @IsDefined()
  @IsNotEmpty()
  objectId: string;
}

Update: When I use HTTP POST with Content-Type as application/json, then it works.

I expect to have a simple config key at Controller to switch into FormData.