In this post , we will learn how to use one of the most powerful commands of Linux , curl which may be used to query web servers and API specific applications. Here are few practical examples of curl command
Use case 1 : To perform a GET request for a web or API URL
Syntax : curl --get "End point URL"
Example : curl --get "https://shareolite.com/feeds/poster/default?alt=json&type=1&filter=latest"
Use case 2 : To perform a POST request for a web or API URL
Syntax : curl "End point URL"
Example : curl "https://shareolite.com/feeds/poster/default?alt=json&type=1&filter=latest"
Use case 3 : POST data to a Web server or API URL
Syntax : curl --data "data to be posted" URL
Example : curl --data "client_id=1&client_name=shareolite" "https://shareolite.com/api"
Use case 4 : POST JSON data to a Web server with body content in a file
Syntax : curl -H "Content-Type: application/json" --data @filename URL
Example : curl -H "Content-Type: application/json" --data @body.json http://10.20.30.40/api/feed
In the body.json file above , copy the JSON content
Use case 5 : POST data with Basic Authentication
Syntax : curl --data "data to be posted" --user name:password URL
Example : curl --data "client_id=1&client_name=shareolite" --user "testuser:testpassword" "https://shareolite.com/api"
Use case 6 : Print header information also using option -v
Syntax : curl -H "Content-Type: application/json" --data @filename URL -v
Example : curl -H "Content-Type: application/json" --data @body.json http://10.20.30.40/api/feed -v
In the body.json file above , copy the JSON content
Use case 7 : Set Time out for connection request using --connect-timeout
Syntax : curl --connect-timeout (value in seconds) URL
Example :
curl --connect-timeout 20 https://shareolite.com ( 20 seconds )
curl --connect-timeout 3.14 https://shareolite.com ( 3.14 seconds )