1
votes

I'm using Nest framework and trying to make a Post request that gets a file and data here's what I did

  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@Body('data') addRefundDto:AddRefundDto,@UploadedFile() file) {
    console.log(addRefundDto);
    console.log(addRefundDto.price);
    console.log(addRefundDto.refundType);
    return '   ';
  }

which prints :

{ "refundType":"test", "currency":"usd", "price":58.5, "flightId":4, "iAiCard":true }

undefined

undefined

It's like the dto gets the json as text (I also tried calling @Body()instead of @Body('data'))

I removed "Content-Type","application/json" from the request in PostMan so I can upload the file, is there anyway to solve this nicely ?

Thanks a lot !

2

2 Answers

1
votes

You could always JSON.parse(addRefundDto) to put it back in JSON format. the body-parser package will only parse the data if you send it as application/x-www-form-urlencoded or application/json, otherwise you'll need to parse it yourself (in this case simply with JSON.parse()).

0
votes

Maybe for someone the solution below will work.

You can use DTO without nested objects.

export class SomeDto {
    @ApiProperty()
    public readonly field1: string;

    @ApiProperty()
    public readonly field2: string;

    @ApiProperty({
        format: 'binary',
        type: 'string'
    })
    public readonly file: IMulterFile;
}