HostOnNet Blog

Making POST request with curl

For an API request, i need to sent POST request with following data.

POST https://api.ctl.io/v2/authentication/login
Content-Type: application/json

{
  "username":"demouser1",
  "password":"mypassword"
}

This can be done with following curl command

curl --request POST -H "Content-Type: application/json" --data '{"username":"demouser1","password":"mypassword"}' https://api.ctl.io/v2/authentication/login

Here –request POST tell curl to use POST method for the request. -H specify the header. –data specify POST data to be sent to serer.

Example

boby@hon-pc-01:~ $ curl --request POST -H "Content-Type: application/json" --data '{"username":"hostonnet","password":"assNoPword1"}' https://api.ctl.io/v2/authentication/login
{"userName":"hostonnet","accountAlias":"ZVJ0","locationAlias":"NY1","roles":["AccountAdmin"],"bearerToken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46YXBpLXRpZXIzIiwiYXVkIjoidXJuOnRpZXIzLXVzZXJzIiwibmJmIjoxNDkxODExNTk0LCJleHAiOjE0OTMwMjExOTQsInVuaXF1ZV9uYW1lIjoidG9tbXk0MzA4NyIsInVybjp0aWVyMzphY2NvdW50LWFsaWFzIjoiWlZKMCIsInVybjp0aWVyMzpsb2NhdGlvbi1hbGlhcyI6Ik5ZMSIsInJvbGUiOiJBY2NvdW50QWRtaW4ifQ.D2sdg029yCUyX6gDxxQwbgcTuEoImezIj7vaiytrYPM"}boby@hon-pc-01:~ $

Download Password protected files using curl

curl -u USERNAME:PASSWORD http://url/to/password/protected/file

Posted in Linux