Operations 13 min read

Comprehensive Guide to Using the curl Command-Line Tool

This article provides a detailed tutorial on the curl command-line tool, covering its purpose, common options such as -A, -H, -b, -c, -d, -F, -L, -k, and advanced usage like file uploads, proxy settings, and response testing, with practical code examples for each.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Comprehensive Guide to Using the curl Command-Line Tool

Introduction

curl is a widely used command-line utility for making HTTP requests, acting as a client URL tool with dozens of options that can replace graphical tools like Postman.

Common Options

2.1 -A and -H (User-Agent and custom headers)

Use # curl https://www.example.com for a simple GET request. The -A flag sets the User-Agent header, while -H allows arbitrary header specification.

# -A parameter specifies the client User-Agent header
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com

# Remove User-Agent header
curl -A '' https://google.com

# Set a custom User-Agent header
curl -H 'User-Agent: php/1.0' https://google.com

2.2 -b (Send cookies)

The -b option sends cookies to the server, either directly or from a file.

# Send a single cookie
curl -b 'foo=bar' https://google.com

# Send multiple cookies
curl -b 'foo1=bar;foo2=bar2' https://google.com

# Read cookies from a file
curl -b cookies.txt https://www.google.com

2.3 -c (Save cookies)

Store server‑set cookies into a file for later reuse.

# Save cookies to a file
curl -c cookies.txt https://www.google.com

2.4 -d and --data-urlencode (POST data)

Use -d to send POST data; --data-urlencode automatically URL‑encodes the payload.

# Simple POST with form data
curl -d 'login=emma&password=123' -X POST https://google.com/login

# Read data from a file
curl -d '@data.txt' https://google.com/login

# URL‑encode data
curl --data-urlencode 'comment=hello world' https://google.com/login

2.5 -H (Add arbitrary HTTP headers)

Specify additional request headers such as Accept-Language or Content-Type .

# Add Accept-Language header
curl -H 'Accept-Language: en-US' https://google.com

# Add multiple headers
curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

# Send JSON with appropriate Content-Type
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

2.6 -F (Upload files)

Upload binary files using multipart/form‑data.

# Upload a file
curl -F '[email protected]' https://google.com/profile

# Specify MIME type
curl -F '[email protected];type=image/png' https://google.com/profile

# Rename the uploaded file
curl -F '[email protected];filename=me.png' https://google.com/profile

2.7 -k (Skip SSL verification)

Disable certificate validation for HTTPS requests.

# Ignore SSL certificate errors
curl -k https://www.example.com

2.8 -L (Follow redirects)

Make curl follow HTTP 3xx redirects automatically.

# Follow redirects
curl -L http://codebelief.com

2.9–2.11 (Saving output)

Various ways to save responses: shell redirection, -o (lowercase) for a specific filename, -O (uppercase) to keep the remote filename, and patterns for batch downloads.

# Shell redirection
curl http://www.linux.com >> linux.html

# Save to a specific file
curl -o linux.html http://www.linux.com

# Save using remote filename
curl -O http://www.linux.com/hello.sh

# Batch download with brace expansion
curl -O http://www.linux.com/{hello,bb}/dodo[1-5].JPG

2.12 Testing HTTP response codes

Use -w %{http_code} to output the status code, useful in scripts.

# Print HTTP status code only
curl -o /dev/null -s -w "%{http_code}" www.linux.com

2.13 Proxy support

Set a proxy server with -x .

# Use proxy at 192.168.100.100:1080
curl -x 192.168.100.100:1080 http://www.linux.com

2.14 Save response headers

Store response headers in a file using -D .

# Save headers to cookied.txt
curl -D cookied.txt http://www.linux.com

2.15 Fake Referer (anti‑hotlinking)

Set the Referer header with -e to bypass hotlink protection.

# Set custom referer
curl -e "www.linux.com" http://mail.linux.com

2.16 File upload

Upload files via FTP using -T .

# Upload via FTP
curl -T dodo1.JPG -u username:password ftp://www.linux.com/img/

2.17 Show errors

Fail silently on HTTP errors with -f and customize output with -w .

# Fail on HTTP errors
curl -f http://www.linux.com/error

2.18 Show HTTP headers

Display only headers with -I or both headers and body with -i .

# Show headers only
curl -I http://www.codebelief.com

# Show headers and body
curl -i http://www.codebelief.com

Conclusion

The guide covers a broad set of curl functionalities, from basic GET requests to complex multipart uploads, proxy configuration, header manipulation, and response testing, providing ready‑to‑use command snippets for developers and system administrators.

networkHTTPAPIcommand linecurldownloadupload
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.