Question- How to POST JSON data with curl? How to POST JSON data with curl command line? How do I POST JSON data on Linux terminal?
You can add -H "Content-Type: application/json"
header value to Post the JSON data to curl command line.
For example, I have an API URL https://api.example.com/v2/login
, that is used to authenticate the application. Now passing the username and password in JSON format using the curl command line tool.
curl -X POST -H "Content-Type: application/json" \ -d '{"username":"abc","password":"abc"}' \ https://api.example.com/v2/login
You can also write the username and password in a user.json file. Now use this file to pass the JSON data to curl command line.
curl -X POST -H "Content-Type: application/json" \ -d @user.json \ https://api.example.com/v2/login
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.