Skip to content

13. Curl Fundamentals

Overview

curl (Client URL) is a command-line tool used to transfer data to or from a server using various protocols. It is widely used for:

  • Testing APIs

  • Downloading files

  • Debugging network issues

  • Web requests in automation and cybersecurity


Supported Protocols

curl supports multiple protocols, including:

  • HTTP

  • HTTPS

  • FTP

  • SFTP

  • Telnet


Basic Syntax

curl <URL>

Fetch Website Content

curl https://www.example.com

Explanation

  • Sends a GET request

  • Displays the HTML/content of the webpage in the terminal


Save Output to File

curl -o /path/output.html https://www.example.com

Explanation

  • -o → save output with custom file name and path

Download Files

Save with Custom Name

curl -o file.zip https://example.com/file.zip

Save with Original Name

curl -O https://example.com/file.zip

Explanation

  • -O → saves file using the name from URL

Handle Redirects

curl -L https://example.com

Explanation

  • -L → follows redirects automatically

  • Required when URLs redirect to another location


View Response Headers

curl -I https://example.com

Explanation

  • -I → fetch only HTTP headers

  • Useful for checking server response

Example Output

HTTP/1.1 200 OK
Content-Type: text/html
Server: nginx

Verbose Mode (Debugging)

curl -v https://example.com

Explanation

  • Shows detailed request/response info

  • Includes:

    • Request headers

    • Response headers

    • TLS handshake

    • Connection details


Send POST Request (Data Submission)

curl --data "key=value" https://example.com

Example: Login Request

curl --data "log=admin&pwd=passwd" https://example.com/login

Explanation

  • Sends POST request with parameters

  • Used in API testing and form submission


Common curl Options

Option Description
-o Save output with custom name
-O Save with original filename
-L Follow redirects
-I Show response headers
-v Verbose/debug mode
--data Send POST data

Practical Examples

Download a File

curl -O https://example.com/file.txt

Save Webpage Content

curl -o page.html https://example.com

Check Server Response

curl -I https://example.com

Debug Connection

curl -v https://example.com

Send API Request

curl --data "username=test&password=123" https://example.com/api

Important Notes

  • Always use -L when dealing with redirects

  • Use -v for troubleshooting network issues

  • Avoid sending sensitive data over HTTP (use HTTPS)

  • Combine curl with other tools for automation


Summary Table

Command Purpose
curl URL Fetch webpage content
curl -o file Save output
curl -O URL Download file
curl -L URL Follow redirects
curl -I URL Show headers
curl -v URL Debug request
curl --data Send POST request

Conclusion

curl is a powerful and versatile tool for:

  • Web interaction

  • API testing

  • File transfer

  • Network debugging

Mastering curl is essential for developers, system administrators, and cybersecurity professionals working in Linux environments.