Home:ALL Converter>Using multipart/form-data in Nestjs

Using multipart/form-data in Nestjs

Ask Time:2021-10-29T12:14:13         Author:user17241551

Json Formatter

I have a requirement of uploading a pdf file to the a third-party API to do that what we are using a controller and then sending its buffer to the endpoint in multipart/form-data but It's not working. What am i doing wrong??

I have my controller defined like

    @Post('/upload')
    @UseInterceptors(FileInterceptor('file'))
    async uploadDocument(@UploadedFile() file: Express.Multer.File){
        await this.httpRequestService.uploadDocument(file)
        return [
            Translation.translate('Create_Success_Message', {
                Entity: 'File'
            }),
            {
                file
            }
        ]
    }

This controller calls a service called uploadDocument which looks like:

async uploadDocument(file){
        try{
        const formData = new FormData()
        formData.append('file', file)
        const response = await this.httpService.post(`urlWhereItHits`,
        formData, 
        {   
            headers:{ 
                "X-AppSecretToken":"appsecrettoken",
                "X-AgreementGrantToken":"agreementgranttoken",
                'Content-Type' : 'multipart/form-data'
            },
            
        }).toPromise();
        }
        catch(error){
            console.log(error)
        }
}

Now i don't understand what's happening wrong here. I get error saying
TypeError: source.on is not a function at Function.DelayedStream.create (C:\Users\Tushar\Squire-backend\node_modules\delayed-stream\lib\delayed_stream.js:33:10)

Author:user17241551,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69763553/using-multipart-form-data-in-nestjs
yy