Curl command

Curl is a command line tool powered by Libcurl library to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE). Since curl is designed to work without user interaction it is the prefered tool for automation.

You can use the curl command from your terminal, as it comes pre-installed on most Linux based Operating Systems.

Curl command use cases: Requesting a page:
If no argument is explicitly passed, curl command invokes an HTTP get request. This command will output the HTTP response of the URL in question displaying the static content of the page similar to the view page source option in a browser.

curl www.youtube.com

Get HTTP headers:
The -I option is used to tell curl to only fetch the HTTP headers of a particular page or a document from the server.

curl -I https://www.google.com

HTTP/2 200 
content-type: text/html; charset=ISO-8859-1
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
date: Tue, 23 Mar 2021 12:36:28 GMT
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
expires: Tue, 23 Mar 2021 12:36:28 GMT
cache-control: private
set-cookie: 1P_JAR=2021-03-23-12; expires=Thu, 22-Apr-2021 12:36:28 GMT; path=/; domain=.google.com; Secure
set-cookie: NID=211=aKrey1_fojp5LY6qIDsIJP7CH6Byfrgpu1lnpcOhVXB9KLPXGI5u_JF3jA7zbBUIRbFhHW09DG_0EJjpPKLVEW3DFUVr93mSE5CiIieCP1X_H8TonkZ5PSJ8cDicBFJB49fDRD6o9BmB54YE7W1dZJbpjL3HFQCdoinIHlPzFZA; expires=Wed, 22-Sep-2021 12:36:28 GMT; path=/; domain=.google.com; HttpOnly
alt-svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

Download a file:
There are two options of downloading and saving a file:

  1. Download a file and save it with the same name.
    curl -O https://testdomain.com/testfile.tar.gz
    
  2. Download a file and save it with a custom name.
    curl -o custom_file_name.tar.gz https://testdomain.com/testfile.tar.gz
    

Ignore invalid or self-signed certificates:
When testing an API endpoint that is running on a testing instance. Every SSL connection that curl makes is verified by default. To prevrent curl from verifying the validity of the SSL certificate and establish an insecure connection for testing, the -k or - -insecure option can be used.

curl -k https://localhost/my_test_endpoint

Make a POST request:
When testing an API, you need to send some parameters, especially when making a POST request. When using curl to test a POST request, the data is in JSON formart, key value pair.

 curl  -H 'Content-Type: application/json' --data '{"key":"value1" "key":"value2"}' http://www.test.com

Specifying a type of request(PUT,POST,GET,DELETE): If you want to just update a record instead of recreating, curl has an option that comes in handy for this. The -X or - -request option lets you specify the type of HTTP request method to use. i.e:

curl -X 'PUT' -d '{"key":"value1","key":"value3"}' \http://test.com/1

By default curl makes an HTTP get request.

Include the Basic Auth:
To test an API endpoint which is behind authentication, you can pass in the credentials when making the request using the -u or -user option.

curl -u user:password https://my-test-api.com/endpoint1

Upload a file:
Curl with the -F option emulates a filled-in form when the user has clicked the submit button. This option causes curl to POST data using the Content-Type multipart/form-data.

curl -F fileX=@/path/to/fileX upload_URL or PATH