The curl is a free and open-source command-line tool for transferring data using many protocols such as HTTP, SFTP, SMTP, TELNET, and more. This software is very popular among developers and DevOps around the world. In this article, we will focus on how to POST JSON data using curl.
2. POST JSON data with curl on Linux
To send JSON data that will be properly parsed on the server-side, using curl, we need to add the Content-Type header with application/json value to our request.
We can do it using -H or --header parameter, according to the document curl-H:
In our case we need to set application/json header, to do so we will use the following parameter:
The example curl command that POST JSON data could look as follows:
In that example we used parameters such as:
--request POST - that is optional, because -d or --data parameters implies a POST request,
--data '[json]' - parameter with JSON data we want to send,
http://localhost:8080/api/customer - this is an URL where we want to sent prepared JSON.
2. POST JSON data with curl on Windows
Things are a little bit different on the Windows OS. For windows, single quotes around JSON data didn't work correctly.
So, we should not use a single quote at all, and double quotes should be escaped like in the following example:
3. POST JSON data from a file using curl
In case you want to POST a JSON from a file, you will need to use the @ parameter with the name of that file.
Let's check the example command:
Note that in this example we used --data-binary instead of --data param. That param indicates that data will be sent as-is. The --data parameter strips CR and LF from the input.
4. Post JSON using special --json option
The --json parameter works as a shortcut for passing on these three options:
--header "Content-Type: application/json"
--header "Accept: application/json"
This is a new option introduced by cURL in version 7.82.0.
With --json parameter we could simplify our command into:
5. Test sending JSON data with curl
To test if POST requests generated by curl are correct we will use a spring-boot application with a simple REST API.
Application is configured to print all requests with headers, query parameters, etc.
The CurlController is responsible for handing all requests to our server:
The Customer with Address will be our testing objects, on which we will check if POST requests with JSON data are correct, and spring-boot can parse it.
5.1. POST Customer data in JSON to the Spring Boot application server
Let's POST a JSON data with Customer information:
Server logs the following output on the console:
As you can see Spring Boot property parse input JSON and create an instance of the Customer object with the Address list.
5.2. Post JSON data from a file
Let's create a sample-file.json file with the following content:
To POST data from that file we will use the following command:
Server output is as follows:
In this article, we focused on preparing the correct POST command using curl that will send JSON data to a Spring Boot server.