CURL COMMANDS
● CURL stands for Client URL
● CURL Commands can be ran in CMD/Terminal, if they want to share the API
Details
● API Client : http request
● Useful for the documentation which gives the overall picture for the API that we
are working
● Lightweight commands, where we need to follow some commands
**For MAC, LINUX —> CURL is installed by default
Steps to install the CURL:
● Download the CURL Installer from : https://curl.se/download.html
● Select the CURL binary based on your operating system
● Unzip the File
● Then navigate to the bin path
● Copy the Bin path and add it to the environment variables
● Then Open the Cmd prompt and type curl www.google.com
If we hit curl www.google.com → It will display the DOM of the Google Page.
When we put it in the browser, it parses the HTML page and display the UI
Properly
CURL Explanation:
curl -i -H "Accept:application/json" -H "Content-
Type:application/json" -H "Authorization: Bearer ACCESS-TOKEN" -
XGET "https://gorest.co.in/public/v2/users"
-H - represents the Header in the key-value pair
-X - represents the HTTP method or kind of API Request (GET, POST, PUT,DELETE,
OPTION, HEAD, PATCH) followed by API URL
-i - include the response headers while generating the response
Once we hit the above CURL Command in CMD, then we can see the response in the
command, which does not have a great look and feel although
Another Example:
curl -i -H "Accept:application/json" -H "Content-
Type:application/json" -H "Authorization: Bearer ACCESS-TOKEN" -
XPOST "https://gorest.co.in/public/v2/users" -d '{"name":"Tenali
Ramakrishna", "gender":"male",
"email":"tenali.ramakrishna@15ce.com", "status":"active"}'
-H - represents the Header in the key-value pair ( alternatively –header can be passed
instead of -H)
-X - represents the HTTP method or kind of API Request (GET, POST, PUT,DELETE,
OPTION, HEAD, PATCH) followed by API URL (alternatively we can pass –request
instead of -x)
-i - include the response headers while generating the response (alternatively we can
use –include)
-d - Data (API Body Request) and pass the JSON BODY in ‘ ’ (alternatively we can use
–data)
curl -i -H "Accept:application/json" -H "Content-
Type:application/json" -H "Authorization: Bearer ACCESS-TOKEN" -
XPOST "https://gorest.co.in/public/v2/users" -d '{"name":"Tenali
Ramakrishna", "gender":"male",
"email":"tenali.ramakrishna@15ce.com", "status":"active"}'-o
<file_name or file_path>
-H - represents the Header in the key-value pair ( alternatively –header can be passed
instead of -H)
-X - represents the HTTP method or kind of API Request (GET, POST, PUT,DELETE,
OPTION, HEAD, PATCH) followed by API URL (alternatively we can pass –request
instead of -x)
-i - include the response headers while generating the response (alternatively we can
use –include)
-d - Data (API Body Request) and pass the JSON BODY in ‘ ’ (alternatively we can use
–data-raw)
-o - export the response to the particular file ( we can export in text file, JSON, PDF
etc).
CURL Commands does not help you in API Testing but a lightweight command
for Sharing the API Details
If we add the CURL Commands in the POSTMAN, it will automatically parse the details.
It will work with all the API Tools majorily
**Order of the headers, data, HTTP Methods does not matter in CURL Commands
DEFAULT HTTP METHOD for the CURL is GET API CALL
HTTP METHODS SHOULD BE WRITTEN IN CAPITAL LETTERS
****************************************
****************************************
********
- L - Location —> Mostly used in the URL Redirection (with a response code of
301(moved)/302(Found))
Alternatively we can use –Location instead of -L
Maximum value of redirection attempts is 50
URL Redirection is a GET Call
****************************************
****************************************
********
CURL COMMAND FOR BASIC AUTH:
● curl -X GET -i -u “username:password” “<API_URL>”
● Along with -u we need to give username or (username and password)
● If we do not want to give my password:
curl -X GET -i -u “username” “<API_URL>” , then press the ENTER Key
then we have to enter the password to proceed for further operations
CURL COMMAND FOR SINGLE FILE-UPLOAD:
● curl -XPOST -i –form ‘file=@”<file-path>”’ “<API_URL>”
CURL COMMAND FOR MULTIPLE FILE-UPLOAD:
● curl -XPOST -i –form ‘file1=@”<file-path>”’ ‘file2=@”<file-path>”’ “<API_URL>”
^ -> Represents a new line in CURL in Windows
\ -> Represents a new line in CURL in MAC