Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept form-data in Nest.js

I have written auth routes in Nestjs and wanted to use it with the form-data. I got it working with URL-encoded-form-data, JSON, text, but not receiving anything in the body when I use form-data and really want it to work with form-data as on the front-end I am hitting the route with form-data. I have tried every way I could find on the web, but none of them helped me in this case. so after hours of searching and trying when I didn't get any lead I am posting my question here. Any Kind of Help is appreciated.

Code of signup endpoint here is my code of signup endpoint

Main.ts configurations my Main.ts configurations

empty body I am getting on postman empty body I am getting on postman

like image 863
usama_akram Avatar asked Sep 13 '20 14:09

usama_akram


1 Answers

NestJS provides a built-in multipart/form-data parser which you can access using a FileInterceptor.

Here's an example of how you'd read a single file (as you've shown in your screenshot):

@Post('signup')
@UseInterceptors(FileInterceptor('<name of file here - asdasd in your screenshot>'))
signup(@UploadedFile() file, @Body() body) {
  console.log(file);
  console.log(body);
}
like image 179
eol Avatar answered Nov 06 '22 15:11

eol