Quick take: The curl command transfers data to and from servers. Download with curl -O url, follow redirects with -L, send headers with -H, and POST data with -d. It is the go-to tool for testing APIs from the terminal.

Introduction

The curl command transfers data over HTTP, HTTPS, FTP, and many other protocols. Administrators use it to download files and check endpoints; developers use it constantly to test REST APIs without leaving the terminal. Its flags map directly to the parts of an HTTP request — method, headers, body, and authentication.

This guide covers downloading files, inspecting responses, and building API requests with headers, data, and credentials.

Syntax

The basic syntax of the curl command is:

curl [OPTIONS] URL

Common Options and Parameters

The most useful options and parameters for the curl command:

OptionDescription
-OSave the file using its remote name.
-o FILESave the output to a specific filename.
-LFollow HTTP redirects.
-IFetch only the response headers (HEAD request).
-X METHODSet the HTTP method (GET, POST, PUT, DELETE).
-d DATASend POST data in the request body.
-H 'Header: val'Add a request header (repeatable).
-u user:passSend HTTP basic authentication.
-sSilent — hide progress and errors.
-kAllow insecure TLS (skip certificate checks).
-A 'agent'Set a custom User-Agent string.

Practical Examples

Real curl commands you can run today:

# Download a file with its remote name
curl -O https://example.com/app.tar.gz
# Save to a chosen filename and follow redirects
curl -L -o app.tar.gz https://example.com/latest
# Show only response headers
curl -I https://example.com
# GET a JSON API endpoint
curl https://api.example.com/v1/users
# POST JSON to an API
curl -X POST -H 'Content-Type: application/json' -d '{"name":"Irfan"}' https://api.example.com/users
# Send basic authentication
curl -u admin:secret https://api.example.com/admin

Testing REST APIs with curl

curl is the de facto tool for exercising HTTP APIs from the terminal. Its flags map directly onto the parts of a request, so you can reproduce almost any client call.

# GET with a bearer token, pretty-printed via jq
curl -s -H 'Authorization: Bearer $TOKEN' https://api.example.com/users | jq

# Create a resource with JSON
curl -X POST https://api.example.com/users \
  -H 'Content-Type: application/json' \
  -d '{"name":"Irfan","role":"admin"}'

# Update with PUT, delete with DELETE
curl -X PUT  https://api.example.com/users/7 -d '{"role":"editor"}'
curl -X DELETE https://api.example.com/users/7

Piping the response into jq turns dense JSON into readable, queryable output, making curl plus jq a complete API-testing workflow.

Debugging Requests with curl

When a request misbehaves, curl can show exactly what is happening on the wire. The -v (verbose) flag prints the request and response headers, while -w reports timing and status without the body.

# See the full request/response exchange
curl -v https://example.com

# Print just the HTTP status code
curl -s -o /dev/null -w '%{http_code}\n' https://example.com

# Measure how long the request takes
curl -s -o /dev/null -w 'time: %{time_total}s\n' https://example.com

These checks quickly distinguish a DNS or TLS problem from an application error — invaluable when a service is up but a specific endpoint is failing.

Tips and Best Practices

  • Add -s for clean output in scripts, and combine with -S (-sS) so errors still show while progress is hidden.
  • Use -I to check status codes and headers quickly — perfect for verifying redirects or caching.
  • Pipe JSON responses into jq for readable, queryable output: curl -s url | jq.

Final Thoughts

curl is the Swiss-army knife of HTTP from the command line — downloading files, checking endpoints, and exercising APIs with full control over method, headers, body, and auth. Learn -O, -L, -I, -H, and -d, and you can test and automate almost any web request. For mirroring sites or resumable downloads, pair it with wget.

FAQ: curl Command in Linux

How do I download a file with curl?+

Use curl -O url to save it with its remote name, or curl -o name url to choose the filename. Add -L to follow redirects to the real file.

How do I send a POST request with curl?+

Use -X POST with -d for the body and -H for headers, for example curl -X POST -H 'Content-Type: application/json' -d '{...}' url.

How do I add headers to a curl request?+

Use -H once per header: curl -H 'Authorization: Bearer TOKEN' -H 'Accept: application/json' url.

How do I see only the HTTP status and headers?+

Use -I to make a HEAD request that returns just the response headers, including the status code.

What is the difference between curl and wget?+

curl is built for flexible single requests and APIs, supporting many protocols and request options. wget specialises in downloading, with easy recursive mirroring and resumable downloads.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support