Register Login

Post JSON data with cURL

Updated Mar 12, 2024

This comprehensive guide discusses commands to send JSON data with HTTP POST Request in the cURL command. Before sending JSON data with the HTTP POST request method in the cURL command, users must set the Content-Type header as application/json using the cURL -H or --header. Here are some examples and explanations for using cURL for sending POST requests with a JSON body.

What is cURL?

The cURL is a Command-line Interface tool allowing users to request and transfer data over a URL following specified protocols. It provides flexibility and control to exchange data between devices and servers. It uses the libcURL client-side library that supports various protocols like SMTP, HTTPS, etc.

Examples of CURL to POST JSON data

Example 1: POST data with JSON file

First, users will create a JSON file with a .json file extension. Here, users will add data of their choice.
For instance, the demo.json file contains the following content/data:

  "InsuranceCompanies":  {
    "Time":"Feb 2019",
    "Top Insurance Companies":[
        "No": "1",
        "Name": "Berkshire Hathaway ( BRK.A)",
        "Market Capitalization": "$308 billion"
        "No": "2",
        "Name": "China Life Insurance (LFC)",
        "Market Capitalization": "$80  billion"

It is the json data we want to send in the request body. When users are working with large content for a post body, they need to create a JSON file and upload the contents directly to the HTTP server. Putting complex JSON data into a file and fetch the file by its name in the command instead of adding these large and complicated JSON data in the Windows command line is efficient.


cURL --header "Content-Type: application/json" \
--request POST  \
--data '@demo.json' https://localhost:8080/products/add | json_pp


Example 2: CURL POST request with \

If you are running this command in windows, then escape " with \ as mentioned below:


cURL -H "Content-Type: application/json" -d "{ \"Name\": \"cURL POST\", \"body\": \"JSON post request\", \"Phone Number\": 0000000000 }" -X POST https://localhost:8080/posts/add


Note: Remember, by default, -d (or --data) sends the Content-type application/x-www-form-urlencoded, but not acceptable on Spring's side. Thus, the effective way is to use the -h or --header to specify the content type.

Example 3: POST with JSON

Users can use the --json option to make the JSON content more readable. Applying the --json option no longer requires the -H "Content-Type: application/json." It also eradicates the need for -X POST or --request POST in the command. The cURL internally controls and executes the above options. The --json option only includes the JSON data as the post body apart from the request URL.


cURL --json '{"Name": "cURL POST", "body": "JSON post request", "Phone Number": 0000000000}' http://localhost:8080/posts/add | json_pp


Let us create the same command with a JSON file, i.e., demo.json:

cURL --json '@demo.json' https://localhost:8000/products/add | json_pp


Example 4: Multiline request in Command Line in Windows

Users can install git and use git bash for executing the cURL multiline commands in their operating system. The git bash application constructs multiline JSON data with little effort.


We hope this article gives all the critical points to post JSON data with cURL in the command line. Users can use both ways, i.e., directly specifying the JSON content in the command or adding JSON file names in case of complex and large JSON data. Also, you should pay attention to all the correct formats in the cURL command, including -d or --data and -H.