Question- How to send a POST Request with a JSON Body using the Curl Command Line?
The `curl`
command line utility is a powerful tool for making HTTP requests. It can be used to send a variety of different HTTP requests, including POST requests with a JSON body. Here’s how you can use curl to send a POST request with a JSON body:
- Create a JSON file
Create a JSON file that contains the data you want to send in the request body. For example, let’s say you have a file named
`data.json`
with the following contents:1234{"name": "John Doe","age": 25} - Curl POST Data
Use the curl command to send a POST request with the JSON data. The
`-X`
option specifies the request method (in this case, POST), and the`-H`
option adds an HTTP header (in this case,`Content-Type: application/json`
to specify that the request body is in JSON format). The -d option specifies the request body, and the`@`
symbol tells curl to read the data from a file.Here’s the command to send the POST request with the JSON data:
curl -X POST -H "Content-Type: application/json" -d @
data.json http://example.com/endpoint - Curl POST Data with Response Header
If the request is successful, the server will return a response. You can use the
`-i`
option to include the response headers in the output, or the`-o`
option to save the response to a file.Here’s an example of using the
`-i`
option to print the response headers:curl -X POST -H "Content-Type:application/json" -d @
data.json http://example.com/endpoint -iAnd here’s an example of using the
`-o`
option to save the response to a file:curl -X POST -H "Content-Type: application/json" -d @
data.json http://example.com/endpoint -o response.txt
That’s all there is to it! With these simple commands, you can use `curl` to send a POST request with a JSON body to a server.
Keep in mind that the JSON data in the request body must be properly formatted and valid, or the request may fail. You can use a tool like JSONLint (https://jsonlint.com/) to validate your JSON data before sending it in the request.
I hope this tutorial has been helpful in showing you how to use `curl` to send a POST request with a JSON body. If you have any questions or need further assistance, don’t hesitate to ask.
5 Comments
how to use “Authentication: XXX”?
I have a case as bellows:
curl -u admin:admin123 -X POST -H ‘Content-Type: application/json’ -d “@a.json” “http://abc.net:7180/api/v33/clusters/CDH_5.14/services/hdfs/snapshots/policies/hdfs_snapshot_p3”
But got 405 error. how to fix it? # grep hdfs_snapshot_p3 cloudera-scm-server.log
2021-01-22 16:44:50,176 INFO scm-web-1371570:com.cloudera.enterprise.JavaMelodyFacade: Entering HTTP Operation: Method:POST, Path:/v33/clusters/CDH_5.14/services/hdfs/snapshots/policies/hdfs_snapshot_p3
2021-01-22 16:44:50,183 INFO scm-web-1371570:com.cloudera.enterprise.JavaMelodyFacade: Exiting HTTP Operation: Method:POST, Path:/v33/clusters/CDH_5.14/services/hdfs/snapshots/policies/hdfs_snapshot_p3, Status:405
I google online, some webpage said not to Allow POST. do not know how to do that. Can anyone help?
Thank you. I’ve been looking for a solution to loading a JSON file into a curl command for days – this has fixed it.
Thank you again!
this is very helpful. thanks a lot !!
Hi Rahul,
Thanks for this valuable information.
I need to send the mail to the testers with the Jenkins URL using my notification API. I am using the same method you mentioned above, Can you tell me how to invoke the URL in JSON data in dynamic way, since the uri will change for each build.
CURL COMAND:
==============
curl -d “@request.json” -H “Content-Type: application/json” -H “Authentication: XXX” -X POST https://<notification API URL/notifications/quick-send
JSON FILE
==========
{
"templateId": "",
"recipientPhoneNumber": "",
"recipientEmail": "[email protected]",
"recipientChannelId": "",
"recipientDeviceType": "",
"subject": "JENKINS BDD TEST",
"message": "HELLO JENKINS",
"substitutionData": {},
"inAppMessage": "",
"inAppSubstitutionData": {},
"extras": {},
"scheduled": "2018-1-28T23:38:21.701Z",
"archiveAfter": "2018-01-28T23:38:21.701Z",
"emailReplyTo": ""
}
URL of Jenkins:
———————
https://jenkins.eastus.cloudapp.azure.com/view/all/job/PojectName/1/cucumber-html-reports/overview-features.html
Hello,
Thanks for update ideas.