Node js Get Form Data from Postman Tutorial

Node js Get Form Data from Postman Tutorial

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

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:

  1. Open Postman and create a new request.
  2. Set the request method to POST.
  3. Enter the URL for your Express application, which should be http://localhost:3000/submit-form.
  4. In the “Body” tab, select “x-www-form-urlencoded” as the data type.
  5. 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.
  6. 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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *