3

I am trying to send file from Client (Angular) to the NestJS same way like it is working now with Java-springboot API.

I am using POST method in NestJS but, unfortunatelly I am not able to got any data from the body :

here is the code :

  @Post('/uploadExportFile')
  uploadAttachment(@Req() req: Request, @Body() attachment: ArrayBuffer): any {
    console.log(attachment);
    return {};
  }

content-type is set in header on Client side, I am not sure if I need to set content-types there ? Content type depends on file mimetype it should be (application/pdf/png/jpeg)..not multiform or what I need to do to achieve that attachment object will not return empty {} .

req.body is undefined

What I need to do with that file is to again change it back to Base64 (in angular it is in Base64) but Java API consumes only byte[] so I need to keep that like it is on FE.

any suggestions what is wrong in this "simple" code?

** EDIT **

====↓ EDIT ↓====

Solution: request.body is undefined is:

NestJS use as default body jsonBody, so in that case you have to override for specific routes that you want to use raw-body, and if raw-body is used insted of jsonBody, then the body from request is not undefined and it contain ArrayBuffer.

What you need to do is something like this;

Create rawBody middleware raw-body.middleware.ts

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response } from 'express';
import * as bodyParser from 'body-parser';

@Injectable()
export class RawBodyMiddleware implements NestMiddleware {
    use(req: Request, res: Response, next: () => any) {
        bodyParser.raw({type: '*/*'})(req, res, next);
    }
}

app.module.ts

export class AppModule implements NestModule {
    public configure(consumer: MiddlewareConsumer): void {
        consumer
            .apply(RawBodyMiddleware)
            .forRoutes({
                path: '/uploadExportFile',
                method: RequestMethod.POST,
            })
            .apply(JsonBodyMiddleware)
            .forRoutes('*');
    }
}

and you need to disable bodyparser in main.ts

const app = await NestFactory.create(AppModule, { bodyParser: false })

in new version of NestJS is introduced new option raw-body but I have no possibility to test that https://docs.nestjs.com/faq/raw-body#raw-body

strakz
  • 101
  • 2
  • 10

1 Answers1

0

frist thing send the content-type application/x-www-form-urlencoded and sure you have add UseInterceptors Like FileInterceptor you can import FileInterceptor

if you need to get buffer try use file.buffer

import {FileInterceptor} from "@nestjs/platform-express";
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
    async upload(@Req() request: RequestWithUser, @UploadedFile() file) {
        if (!file) {
            throw new HttpException('File is required', HttpStatus.BAD_REQUEST);
        }
        // you have file
        return await this.storageService.upload(file, request.user);
    }
Kareem Adel
  • 371
  • 2
  • 10
  • thank you for a try, but I do not got your help...it make no change for me ..FileInterceptor work only with formData it lookslike..and I am not using formData..my request contain only ArrayBuffer of generated pdfFile – strakz Jul 06 '22 at 21:44
  • 1
    but I found the issue, if you are interested you could check original question where the solution is mentioned.. issue is body-parser which is in express apps by default using json-body – strakz Jul 08 '22 at 08:33