I use HttpClient in Angular to send formdata to Nodejs.
resetPasswordRequest(email){
this.httpOptions={
headers: new HttpHeaders({
'Content-Type':'application/x-www-form-urlencoded'
})
}
const formData = new FormData();
formData.append('email',email);
return this.http.post("http://localhost:3001/forgotPassword",formData,this.httpOptions);
}
Later in NodeJS,I have app.use(bodyParser.urlencoded({extended:true}).
I am able to get req.body but in a different format as below:
{ '-----------------------------24875245787704\r\nContent-Disposition: form-data; name':
'"email"\r\n\r\[email protected]\r\n-----------------------------24875245787704--\r\n' }
I am not sure what has been missed. Could you please clarify and help get value of email? I get req.body.email as undefined.
From MDN FormData
:
It uses the same format a form would use if the encoding type were set to
"multipart/form-data"
which explains why you're getting data in that format.
You could use angular's HttpParams
instead:
const formData = new HttpParams();
formData.set('email', email)
return this.http.post("http://localhost:3001/forgotPassword", formData.toString(), this.httpOptions);
toString
gives you urlencoded
format. From docs:
Serialize the body to an encoded string, where key-value pairs (separated by =) are separated by &s
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments