user3185039
user3185039

Reputation:

Access form data node js

I am building API which accepts form data(variable or image). I am passing the following enter image description here

I am just rendering the req as it is and getting

{
    "------WebKitFormBoundarykjCufNDj9Nu9UePk\r\nContent-Disposition: form-data; name": "\"attachment_type_id\"\r\n\r\n0\r\n
    ------WebKitFormBoundarykjCufNDj9Nu9UePk\r\nContent-Disposition: form-data; name=\"firstName\"\r\n\r\nImrn\r\n
    ------WebKitFormBoundarykjCufNDj9Nu9UePk\r\nContent-Disposition: form-data; name=\"lastName\"\r\n\r\nAhmd\r\n------WebKitFormBoundarykjCufNDj9Nu9UePk--\r\n"
}

If I render req.body.attachment_type_id, I get blank response. How can I get data value attachment_type_id? I have to check attachment_type_id before uploading the image.

Upvotes: 1

Views: 5819

Answers (2)

vpage
vpage

Reputation: 71

Add body-parser as a middleware to your express app before defining routes.



// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse application/json
app.use(bodyParser.json());

body-parser populates request data in req.body

Make sure to add correct headers to your request "key" : Content-Type "value" : "application/x-www-form-urlencoded"

Upvotes: 1

JazzBrotha
JazzBrotha

Reputation: 1748

form-data in Postman is mostly used to send large binary data. If you want to simulate sending text from a web form, you should use x-www-form-urlencoded

Upvotes: 1

Related Questions