In this guide we are going to discuss how to send JSON data to HTTP POST Request in cURL command with examples.
To send JSON data using http POST method in cURL command, you need to set Contentent-Type
header with value application/json using cURL -H
or --header
. You need to provide JSON data using --data
or -d
and use -X
or --request
to specify http method as POST.
1. Example 1:
curl -X POST -H "Content-Type: application/json" -d '{"title": "My Post1", "body": "post content", "userId": 9}' https://dummyjson.com/posts/add
Results :
{ "body" : "post content", "id" : 151, "title" : "My Post1", "userId" : 9 }
2. cURL POST request with JSON in Windows:
On Windows, has to escape "
with \
like in the following example. Tested in windows 11.
curl -H "Content-Type: application/json" -d "{ \"title\": \"My Post1\", \"body\": \"post content\", \"userId\": 9 }" -X POST https://dummyjson.com/posts/add
Results:
{ "body" : "post content", "id" : 151, "title" : "My Post1", "userId" : 9 }
3. Multiline request in Command line
Linux\Unix environments following example is the way you can run multi line JSON data.
curl --header "Content-Type: application/json" \ --request POST \ --data '{ "brand" : "Apple iPhone 14", "category" : "smartphones", "description" : "An apple mobile which is nothing like apple", "discountPercentage" : 13.96, "images" : [ "https://dummyjson.com/image/i/products/1/1.jpg", "https://dummyjson.com/image/i/products/1/2.jpg", "https://dummyjson.com/image/i/products/1/3.jpg", "https://dummyjson.com/image/i/products/1/4.jpg", "https://dummyjson.com/image/i/products/1/thumbnail.jpg" ], "price" : 2999, "rating" : 4.71, "stock" : 96, "thumbnail" : "https://dummyjson.com/image/i/products/1/thumbnail.jpg", "title" : "iPhone 14" }' https://dummyjson.com/products/add | json_pp
Results:
{ "brand" : "Apple iPhone 14", "category" : "smartphones", "description" : "An apple mobile which is nothing like apple", "id" : 101, "images" : [ "https://dummyjson.com/image/i/products/1/1.jpg", "https://dummyjson.com/image/i/products/1/2.jpg", "https://dummyjson.com/image/i/products/1/3.jpg", "https://dummyjson.com/image/i/products/1/4.jpg", "https://dummyjson.com/image/i/products/1/thumbnail.jpg" ], "price" : 2999, "rating" : 4.71, "stock" : 96, "thumbnail" : "https://dummyjson.com/image/i/products/1/thumbnail.jpg", "title" : "iPhone 14" }
4. Multiline cURL command request in Windows
We recommend to install git and use git bash
to run cURL multi line commands in windows.
Conclusion
In this guide, we have seen how to post JSON data with cURL in command line with examples.