post json data in curl

How to POST JSON Data in cURL

cURL is a powerful utility tool that allows you to easily transfer & download data in Linux as well as Windows. It also allows you to send different types of data such as username & passwords, when you request a URL. Sometimes you may need to submit JSON data to a website/application/API. In such cases also, you can use cURL tool. In this article, we will learn how to POST JSON data in cURL.


How to POST JSON Data in cURL

Here is the typical syntax of cURL command.

$ curl [options] URL

Let us say you want to post the following JSON data.

'{"username":"xyz","password":"xyz"}'

to website http://www.example.com/api.

There is slight difference in syntax for Linux and Windows systems, so we will look at them separately.


POST JSON Data in cURL in Linux

In this case you need to provide the following 3 options

-H "Content-Type: application/json"
or
--header "Content-Type: application/json"

--request POST

--data '{"username":"xyz","password":"xyz"}' 
or
-d '{"username":"xyz","password":"xyz"}' 

In the above code block, -H option is used to specify the request header, –request option is used to mention the request method, which is POST in this case. -d or –data is used to specify input data. It is important to put single quotes around your JSON data as shown above, since it is a string.

So here is the full curl command.

$ curl -H "Content-Type: application/json" --request POST -d '{"username":"xyz","password":"xyz"}' 

Sometimes your JSON data may be present in a file. In such cases, you can specify JSON filename with -d option, by adding @ its beginning. Here is an example to POST JSON data in cURL using filename product.json.

$ curl -H "Content-Type: application/json" --request POST -d @product.json
OR
$ curl -H "Content-Type: application/json" --request POST -data @product.json


POST JSON Data in cURL in Windows

If you are using cURL on Windows, then here are the options you need to use to POST JSON data in cURL.

-H "Content-Type: application/json"
or
--header "Content-Type: application/json"

-X POST

--data "{\"username\":\"xyz\",\"password\":\"xyz\"}" 
or
-d "{\"username\":\"xyz\",\"password\":\"xyz\"}" 

As you will see above, you need to use -X option to specify request method POST. In the above code block, -H option is used to specify the request header, -d or –data is used to specify input data.

Also single quotes may not work to wrap JSON data. So you will have to enclose it in double quotes. Since you are enclosing your JSON string in double quotes, you will need to escape all double quotes in your key-value pairs, by adding backslash (\) before them.

If you want to send JSON data in cURL in Windows, you can mention the filename after -d or –data option, by adding @ at its beginning. You also need to enclose the filename within double quotes.

$ curl -H "Content-Type: application/json" --request POST -d "@product.json"
OR
$ curl -H "Content-Type: application/json" --request POST -data "@product.json"

In this article, we have learnt how to send JSON data for POST requests in cURL. When you use it in Windows, you need to be careful about using double quotes, instead of single quotes, to enclose JSON data and escaping them within your data.

cURL is a super useful tool that not only allows you to download files & data but also send POST data to websites/applications/APIs. You can embed the above cURL commands within your shell script to automate tasks, or even setup a cronjob, in case you need to regularly send POST data to a specific URL.

Also read:

How to Store JSON to File in Python
How to Handle Multiple Exceptions in Python
How to Make File Executable in Linux
How to Recursively Change Folder Permission in Linux
How to Get Size of Directory in Linux

Leave a Reply

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