How to Post JSON Data using Curl ?
Last Updated :
25 Apr, 2024
One can send the post data using curl (Client for URLs), a command line tool, and a library for transferring data with URLs. It supports various types of protocols. Most of the use cases of the curl command are posting JSON data to a server endpoint.
CURL
cURL stands for ( Client for URLs) and is a command line tool and library for transferring data with URLs. It supports many protocols, including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, LDAP, and TFTP. Daniel Stenberg developed it, cURL is open source and widely used in development and system administration tasks. It is preinstalled in Linux-based distros.
Syntax:
curl [options] [URL]
Options in the CURL tool:
- -X: This option specifies the HTTP method (GET, POST,PUT,DELETE etc).
- -H: This option adds the customer Headers to the request.
- -d: This option sends data in the request body using the POST requests.
- -o: This option write output into a file.
- -O: Downloads the file and uses the remote file name.
- -L: It follows redirects.
- -u: This option provides a username and password for authentication.
JSON
JSON , which stands for JavaScript Object Notatino, is a lightweight data interchange format. It is used for sending the data over the Internet. It is easy to read and write for humans. JSON is often used to transmit data between a server and a web application as an alternative to XML(eXtensible Markup Language).
Example: To demonstrate the simple JSON data.
{
"username": "example_user",
"password": "P@ssw0rd123"
}
Server Code
In this examples, I created a simple server using the Node.js with Express framework. In this code, there is login page where the server check the user password is correct or not. If the user is not correct. Then, It will throw an error otherwise. User will login Successfully.
Step 1: Installing the dependencies
npm install express
Step 2: Installing the nodemon package
npm install nodemon
Step 3: Running the Server using the following command
npm start
Package.json

JavaScript
const express = require('express');
const app = express();
app.use(express.json());
app.post('/login', (req, res) => {
const { username, password } = req.body;
if (username === password) {
res.send("User login Successfully")
.status(200);
}
else {
res
.send("User not Logging ")
.status(401);
}
})
app.listen(5050, () => {
console.log("Server is Running");
})
Using the "-d" or "--data" option in CURL Command
One of the simplest way to post JSON data with cURL using the '-d' or '--data' flag followed by the JSON payload enclosed in single quotes. This method sends the data in the request and is suitable for most use cases.
Example : Sending Post Request in cURL using "-d" option in cURL
curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"admin"}' http://localhost:5050/login
Output:
"User login Successfully"
Sending JSON data from a file
If you have a large JSON object that you want to send in to server. Sending a large JSON file data is easy to send in CURL. Add the '-d' option with an '@' with JSON file, you can use this methods to send the file's contents.
Example: Sending Post Request in CURL with filename.
curl -X POST -H "Content-Type: application/json" -d @data.json http://localhost:5050/login
Output:
"User login Successfully"
Sending JSON data using '--json' option
There is another method is also available in cURL utility for sending JSON. This option available in 7.82.0 version. This version include in option in cURL '--json'.
Example: Sending POST request using '--json'.
curl -X POST -H "Content-Type: application/json" --json {"username":"admin","password":"admin"}' http://localhost:5050/login
Output:
"User login Successfully"
Similar Reads
How to Pass JSON Data in a URL using CURL in PHP ?
In this article, we will see how to pass the JSON Data in a URL using CURL in PHP, along with understanding the different ways for passing the data in a URL through the illustrations. The cURL stands for client URL, which allows us to connect with other URLs & use their responses in our code, i.e.,
7 min read
How to POST a XML file using cURL?
This article explains how to use the cURL command-line tool to send an XML file to a server using a POST request. A POST request is commonly used to submit data to a server. There are two main approaches to include the XML data in your cURL request: Table of Content Reading from a FileProviding Inli
2 min read
How to Post JSON Data to Server ?
In modern web development, sending JSON data to a server is a common task, especially when working with RESTful APIs. JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy to read and write for humans and easy to parse and generate for machines. This article will gu
4 min read
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 create mock servers using Postman
Postman is a comprehensive API platform used by developers; this platform provides a set of tools that support API design, testing, documentation, mocking, and API Monitoring. It also simplifies each step of the API lifecycle and streamlines collaboration, enabling developers to create and use APIs
7 min read
How to create different post request using Node.js ?
A POST request is one of the important requests in all HTTP requests. This request is used for storing the data on the WebServer. For Eg File uploading is a common example of a post request. There are many approached to perform an HTTP POST request in Node.js. Various open-source libraries are also
3 min read
How to test an API using Postman ?
API testing is a software testing type that tends to validate the application programming interfaces. As per Postman API, API testing is confirming that an API is working as expected. It sends requests to an API and monitors the responses to check its behavior to assess the functionality, reliabilit
5 min read
How To Generate Reports Using Postman Tool ?
Postman is a widely used tool for API testing. It offers a user-friendly interface for sending various HTTP requests (GET, POST, PUT, DELETE). One of the best things that we can do in Postman is Generate reports effectively. In this article, we will explore the various approaches to generating repor
3 min read
Using curl to send email
In this article, we shall see how to harness the power of the "curl" command line tool and Gmail SMTP server in order to send an email programmatically using a bash script, "curl" is a versatile and powerful utility command line tool for making HTTP requests. And is primarily known for transferring
3 min read
How to Upload File and JSON Data in Postman?
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 bo
4 min read