How to send FormData from angular to nestjs

Published

I want to send a FormData from angular to nestjs. In my angular component :

let formData = new FormData();
formData.append('studentFile', file)
formData.append('studentName', name)

const headers = new HttpHeaders();
headers.set("Accept", "application/json").set('Content-Type', 'application/json')

this.httpClient.post("http://localhost:3000", formData, { headers }).subscribe(response => { })

When I print formData is not empty.

In my nestjs:

@Post()
create(@Body() model: any) {
  console.log(model) // is null
  return "Response"
}

NOTE: I do not want to use @UseInterceptors(FileInterceptor(...)) because with the help of ftp package I want to save file in the another server not
in a directory of the project …

How can I get studentFile and studentName in the create method.

Any help would be appreciated.

Thanks in advance

Source: Angular Questions

Published
Categorized as angular, nestjs Tagged ,

Answers

You’re trying to send FormData, but you’re telling the server that it’s in the format of application/json. These are not interchangeable, and should not be thought of as such. You either need to send the data as multipart/form-data and use multer manually, or the FileInterceptor which you’ve said you don’t want to do, or you need to send actual JSON data instead of FormData. It doesn’t make sense to mix the two content types.


Savanah Conn

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq