Cyberithub

How to POST JSON data Using curl (2 Best Methods)

Advertisements

In this article, we will see how to post JSON data using curl utility. Now a days there are many UI based applications are available like Postman, Insomnia etc. to POST the JSON data quite comfortably. But it is quite possible that you will end up in a situation where you don't have the facility or let's say an option to install and use any UI application to send GET/POST request then in that case you need to rely on some command line utility like curl to perform your POST operation. Hence it is very important to understand how to use curl to POST JSON data so that whenever needed you will be able to do without any hiccups.

 

What is Curl

Curl is a free and open source command line utility used mostly by developers and system administrators to transfer data using various network protocols like HTTP, HTTPS, FTP, POP3, IMAP, SFTP etc. It is also most popularly used by testers to test their application APIs.

 

What is JSON

JSON, also famously known as JavaScript Object Notation is a lightweight open standard file format and data interchange format used for representing data in human readable format. It is completely language independent yet it can be easily used with almost any programming languages to parse and generate data.

 

What is POST Method

POST is an HTTP standard request method used by the HTTP client to send data over the world wide web to create/update any record or resource on the web server.

 

How to POST JSON data Using curl (2 Best Methods)

How to POST JSON data Using curl (2 Best Methods)

Also Read: How to Install netstat on Ubuntu 20.04 LTS (Focal Fossa)

Well there are multiple ways to send POST data through curl but here we will discuss the most easiest and often used methods. Let's say you have some JSON data like below which you want to POST as a request body using curl utility :-

{
  "vehicle": false,
  "model":[
    "punch"
  },
  "company":[
    "Tata"
  ],
  "engine": 1199,
  "available":[],
  "color":[
    "DAYTONA_GRAY"
  ]
}

 

Method 1: POST Data directly 

Then in general you can use below curl command to POST the above JSON data:-

cyberithub@ubuntu:~$ curl -X POST https://apps.cyberithub.com/service/car -H "Content-Type: application/json" -k -d '
> {
> "vehicle": true,
> "model":[
> "punch"
> ],
> "company":[
> "Tata"
> ],
> "engine":1199,
> "available":[],
> "color":[
> "DAYTONA_GRAY"
> ]
> }'

To know more about curl command usage, you can check 20 Useful curl command in Linux with Examples | How to Use curl in Linux article. If you need to pass some authorization token then you can do that as well by first storing the authorization token in $token variable and then using it like below:-

cyberithub@ubuntu:~$ curl -X POST https://apps.cyberithub.com/service/car -H "Content-Type: application/json" -H "Authorization:Bearer $token" -k -d '
> {
> "vehicle": true,
> "model":[
> "punch"
> ],
> "company":[
> "Tata"
> ],
> "engine":1199,
> "available":[],
> "color":[
> "DAYTONA_GRAY"
> ]
> }'

 

Method 2: POST Data using a File

The other method that you can use is that you can store the entire JSON content in a file let's say testdata and then POST it like below:-

cyberithub@ubuntu:~$ curl -X POST https://apps.cyberithub.com/service/car -H "Content-Type: application/json" -k -d @testdata

Like before, if you have to pass an authorization token then you can pass it like below:-

cyberithub@ubuntu:~$ curl -X POST https://apps.cyberithub.com/service/car -H "Content-Type: application/json" -k -d @testdata

Similarly, if you also have to pass an apikey then you can do that as well using below command:-

cyberithub@ubuntu:~$ curl -X POST https://apps.cyberithub.com/service/car -H "Content-Type: application/json" -H "apikey: XYuhKD879ajkCNMXLB98oQIbdLpn8235eA" -k -d @testdata

Likewise, if you have to you can pass other headers by using -H flag with POST method.

Leave a Comment