NodeJS get value from formData x-www-form-urlencoded

Gayathri

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.

1565986223

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.

edited at
0

Comments

0 comments
Login to comment

Related

Spring - wrong encoding POST request with x-www-form-urlencoded

How to send post request with x-www-form-urlencoded body

How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)?

Post a x-www-form-urlencoded request from React Native

How to perform a GET request with application/x-www-form-urlencoded content-type in Go?

application/x-www-form-urlencoded or multipart/form-data?

How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

Jersey client Post Request with x-www-form-urlencoded Fails

post application/x-www-form-urlencoded Alamofire

file_get_contents('php://input'); with application/x-www-form-urlencoded;

Swift 4 send POST request as x-www-form-urlencoded

cURL send JSON as x-www-form-urlencoded

Convert json to x-www-form-urlencoded

x-www-form-urlencoded in Xamarin

Postman x-www-form-urlencoded returns null value for a POST request even though the input field is filled

ASP.NET Core Fetch POST FormData() application/x-www-form-urlencoded

Post controller recognizes only parameters sent by "from-data" or "x-www-form-urlencoded" from postman

How to POST x-www-form-urlencoded in retrofit

Karate DSL: How to add specific key and value (XML) as x-www-form-urlencoded to body of request?

Javascript: Get param value from x-www-form-urlencoded request

Handling application/x-www-form-urlencoded posts

Jsoup sends application/x-www-form-urlencoded instead of json

Boolean value in the NSDictionary serialized as true/false for x-www-form-urlencoded

How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8?

Get request in java application using application/x-www-form-urlencoded format

how to post body x-www-form-urlencoded using webclient?

How to convert x-www-form-urlencoded string to json in dart?

file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded

OpenApi -API with X-WWW-FORM-URLEncoded as requestBody