If you are creating a REST API for uploading files, data in Node JS Express app and you are trying to post form data using REST API with Postman app but req.body is undefined or body-parser can’t handle multipart/ and an error is occurring.
To resolve postman form-data not working node js error, you can simply use app.use(bodyParser.urlencoded({ extended: true }));.
In this tutorial, you will learn how to post and get form data from postman in node js express applications.
How to Get Form Data from Postman in Node.js Express
Steps to get form data from postman in node.js express app:
- Step 1: Set up a Node.js Express Application
- Step 2: Create App.js File
- Step 3: Create a Route to Handle Form Data
- Step 4: Use Postman to Send Form Data
- Step 5: Parse and Handle the Form Data
Step 1: Set up a Node.js Express Application
If you haven’t already created a Node.js Express application, then you can do it by running the following command on cmd or terminal:
mkdir express-form-data cd express-form-data
Initialize a new Node.js project and install some required modules by running the following command on terminal or cmd:
npm init -y npm install express --save
Step 2: Create App.js File
Next, open your node js express project in any text editor and create a JavaScript file (e.g., app.js
) for node js Express application root directory:
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); const port = 3000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.get('/', (req, res) => { res.send('Welcome to the Express Form Data Tutorial!'); }); app.listen(port, () => { console.log(`Server is running on port ${port}`); });
Next, run the following command on cmd or terminal to start your Node.js Express server:
node app.js
Your Express application is now running on http://localhost:3000
.
Step 3: Create a Route to Handle Form Data
Let’s create a route to handle form data sent from Postman. Add the following code below your existing app.
js file:
app.post('/submit-form', (req, res) => { const formData = req.body; res.json(formData); });
Step 4: Use Postman to Send Form Data
Now, let’s use Postman to send form data to our Express server:
- Open Postman and create a new request.
- Set the request method to
POST
. - Enter the URL for your Express application, which should be
http://localhost:3000/submit-form
. - In the “Body” tab, select “x-www-form-urlencoded” as the data type.
- Add some form data by clicking the “Add Row” button. You can create key-value pairs, where the key represents the form field name and the value represents the data.
- Click the “Send” button to send the POST request.
When you are calling Get Form Data API from Postman app in Node JS and getting the error like multipart form-data is not working on Node JS. So, you can use the body-parser middleware to parse the request body and provide the form data in the req.body object. To use the body-parser middleware, add the following code to your app.use() call:
const bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }));
Step 5: Parse and Handle the Form Data
Back in your Node.js Express application, you’ve already defined a route to handle the form data. When Postman sends the request, your server extracts the form data using req.body
. In this example, we simply respond with the received form data as JSON.
Conclusion
That’s it! You’ve successfully set up a Node.js Express application to get and handle form data from Postman. You can now build more complex applications that involve processing and responding to form submissions.
Recommended Tutorials