Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to Update Data in JSON File using Node?
Next article icon

How to Upload File and JSON Data in Postman?

Last Updated : 07 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Postman is a very famous API development tool used to test APIs. Postman simplifies the process of the API lifecycle to create and test better APIs. Now, while we are working with APIs sometimes we need to send JSON as data or upload a file through API. There may be a scenario where we have to do both tasks in a single API.

Prerequisites:

  • Express Js
  • Postman
  • API

Steps to upload file and JSON data in Postman

Step 1: Create an ExpressJS Application

We need to handle the requests send through Postman, for that we must have a Backend service ready. Here we are using ExpressJS for the Backend.

npm init -y

Step 2: Install the required dependencies.

npm i express multer body-parser

Folder Structure

Screenshot-2024-05-02-210929
Upload File and JSON Data in Postman

Dependencies:

"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.19.2",
"multer": "^1.4.5-lts.1"
}

Step 3: Configure index.js

Here we will define framework as Express and import Multer for handling file uploads and Body Parser for parsing JSON body. The uploaded files will be stored in the uploads folder of the project. We have configured three different endpoints for uploading JSON data, uploading File, uploading JSON and File respectively. These endpoints shows the data on successful operation and return status code as 200 OK and shows error message in case any error occurs.

JavaScript
//index.js  const express = require('express'); const multer = require('multer'); const bodyParser = require('body-parser');  const app = express(); const upload = multer({ dest: 'uploads/' });  // Middleware to parse JSON bodies app.use(bodyParser.json());  // Upload Json Data only app.post('/controller/uploadJson', (req, res) => {     const user = req.body;     res.status(200).send(Json Data Uploaded Successfully         \nJson Data: ${ JSON.stringify(user) }); });  // Upload File only app.post('/controller/uploadFile', upload.single('file'), (req, res) => {     const file = req.file;     res.status(200).send(File Uploaded Successfully         \nFile Name: ${ file.originalname }); });  // Upload Both Json Data and File in a single request app.post('/controller/uploadJsonAndFile',  upload.single('file'), (req, res) => {     const user = req.body.user;     const file = req.file;      try {         const userOriginal = JSON.parse(user);         res.status(200).send(Json Data and File Received             \nJson Data: ${ JSON.stringify(userOriginal) }             \nFile Name: ${ file.originalname });     } catch (error) {         res.status(400).send('Invalid Json Data');     } });  const PORT = process.env.PORT || 8080; app.listen(PORT, () => console.log     (Server running on port ${ PORT })); 

To start the application run the following command.

node index.js

The Express application is configured to run on port number 8080.

Step 4: Test APIs using Postman

Now it's time to check the APIs using Postman application and see if we are being able to upload JSON data and file or not. In Postman all the Controllers are available under,

localhost:8080/controller

Screenshot-2024-04-05-102146

4.1 Upload JSON:

Let's first try sending JSON Data using Postman. All the request will be sent using HTTP POST method. For this use the URL as,

localhost:8080/controller/uploadJson

Click on the Body tab and select raw and then write the JSON formatted data for the user class.

{
"id": 1,
"name": "Thala Dhoni",
"phoneNo": 7777777777
}

Screenshot-2024-04-05-102431

Now click on Send to upload the data.

Output:

Screenshot-2024-04-05-102557

As you can see we got the response code as 200 OK which means operation is successful. We also the received the message we have set in the Controller with the User Details.

4.2 Upload File

For file upload the URL will be,

localhost:8080/controller/uploadFile

Now for file upload, Go to the Body section. Click on form data and write key as file. Select type as File using the dropdown. For Value select a file from your computer.

Screenshot-2024-04-05-110909

Now Click on Send.

Output:

Screenshot-2024-04-05-110918

As you can see in the above picture, it has returned 200 OK status and successful message with File name is received as response.

4.3 Upload File and JSON

Now we will upload the File and JSON data using a single endpoint. For this use the URL as following,

localhost:8080/controller/uploadJsonAndFile

For this also, Go to Body and then select form-data. For file write file as Key, Type as File and in Value choose the File to upload. For JSON Data, write Key as User, Type as Text and in the text box given, write JSON Formatted Data.

JSON Data

{
"id": 2,
"name": "King Kohli",
"phoneNo": 1818181818
}

Screenshot-2024-04-05-111816

Click on Send.

Output:

Screenshot-2024-04-05-111836

And you can see that we got response code as 200 OK and the success message including User information for JSON Data and File name for File upload.


Next Article
How to Update Data in JSON File using Node?
author
subhadipjanacse24
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • JSON
  • Postman-API-Testing

Similar Reads

  • How to Add Data in JSON File using Node.js ?
    JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article. Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
    4 min read
  • How to upload a file in PHP ?
    In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations. Approach: In your "php.ini" file, search for the "file_uploads" parameter and set it to "On" as mentioned below. file_uploads = On In the "index.html" file, the enctype must be multipart
    3 min read
  • How to Update Data in JSON File using Node?
    To update data in JSON file using Node.js, we can use the require module to directly import the JSON file. Updating data in a JSON file involves reading the file and then making the necessary changes to the JSON object and writing the updated data back to the file. Table of Content Using require() M
    4 min read
  • How to Handle file upload in Node with Multer and Postman
    In this article, we will discuss about file upload functionality using Node, Multer and Postman. File Uploading is a significant functionality of any web service to get the data from the users in the form of files. To implement this feature we will use Node JS with Express and Multer library. Finall
    7 min read
  • How to use variables and data files together in Postman ?
    In this article, we are going to learn the process of using variables and data files together in the Postman. Postman is a powerful tool for API testing, offering extensive capabilities for automating tests and handling various API request types. One of its key features is the ability to use variabl
    6 min read
  • How to handle file upload in Node.js ?
    File upload can easily be done by using Formidable. Formidable is a module that we can install on our project directory by typing the command npm install formidableApproach: We have to set up a server using the HTTPS module, make a form that is used to upload files, save the uploaded file into a tem
    3 min read
  • How to Upload Files in JavaScript?
    We upload files using JavaScript, for this we have to capture form elements, gather information from FormData for easier file uploads, intercept submission events, and utilize Fetch API for asynchronous server requests, for enhanced user experience and efficient data handling. ApproachProject Setup:
    1 min read
  • How to add authentication in file uploads using Node.js ?
    There are multiple ways to upload files and apply authentications to them. The easiest way to do so is to use a node module called multer. We can add authentication by restricting users on file uploads such as they can upload only pdf and the file size should be less than 1 Mb. There are many module
    3 min read
  • How to Upload Files in Ruby on Rails?
    Uploading files in a web application is a common requirement, whether it's for user profile pictures, documents, or any other files. Ruby on Rails makes this task straightforward with its built-in tools. In this article, we'll walk through the steps to set up file uploads in a Rails app, from creati
    6 min read
  • How to Create a File Upload Button in HTML?
    Uploading files through an HTML form is essential for many web applications, as it enables users to easily share documents, images, and other types of files. To create a file upload button in HTML, we can use the <input> element with type="file" attribute inside a <form> tag. Creating a
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences