Operations 12 min read

Mastering curl: Essential Command‑Line Options for HTTP Requests

This guide provides a concise reference of curl's most useful command‑line options, explaining how to set headers, handle cookies, send data, control redirects, limit bandwidth, and debug requests, enabling developers and operators to replace GUI tools with a powerful terminal utility.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering curl: Essential Command‑Line Options for HTTP Requests

Introduction

curl is a widely used command‑line tool for making HTTP requests; its name means “client URL”. With dozens of options it can replace graphical tools such as Postman when used skillfully.

-A (User‑Agent)

Specifies the User-Agent header. The default string is curl/[version]. Example:

$ 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

To omit the header: $ curl -A '' https://google.com Alternatively, use -H 'User-Agent: …' to set it directly.

-b (Send Cookie)

Sends a Cookie header. Example: $ curl -b 'foo=bar' https://google.com Multiple cookies can be sent with a semicolon‑separated list or by reading a cookie file:

$ curl -b 'foo1=bar;foo2=bar2' https://google.com
$ curl -b cookies.txt https://www.google.com

-c (Save Cookie)

Writes cookies received from the server to a file:

$ curl -c cookies.txt https://www.google.com

-d (POST Data)

Sends data in the request body and automatically sets Content-Type: application/x-www-form-urlencoded. It also switches the request method to POST, so -X POST can be omitted.

$ curl -d 'login=emma&password=123' -X POST https://google.com/login

Multiple -d flags are concatenated, and a file can be used with @filename:

$ curl -d '@data.txt' https://google.com/login

--data-urlencode

Like -d but URL‑encodes the data automatically:

$ curl --data-urlencode 'comment=hello world' https://google.com/login

-e (Referer)

Sets the Referer header:

$ curl -e 'https://google.com?q=example' https://www.example.com

The same effect can be achieved with -H 'Referer: …'.

-F (Form Upload)

Uploads a file using multipart/form-data:

$ curl -F '[email protected]' https://google.com/profile

Optional MIME type and filename can be specified:

$ curl -F '[email protected];type=image/png' https://google.com/profile
$ curl -F '[email protected];filename=me.png' https://google.com/profile

-G (GET with Data)

Appends data as a query string to the URL, turning the request into GET:

$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

Combine with --data-urlencode for URL‑encoded parameters.

-H (Custom Header)

Adds arbitrary HTTP headers:

$ curl -H 'Accept-Language: en-US' https://google.com
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

JSON payloads can be sent by setting Content-Type: application/json and using -d:

$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

-i (Show Response Headers)

Prints the response headers before the body.

$ curl -i https://www.example.com

-I / --head (HEAD Request)

Sends a HEAD request and displays only the response headers:

$ curl -I https://www.example.com
$ curl --head https://www.example.com

-k (Insecure SSL)

Disables SSL certificate verification:

$ curl -k https://www.example.com

-L (Follow Redirects)

Follows HTTP redirects automatically:

$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet

--limit-rate

Limits transfer speed, useful for simulating slow networks:

$ curl --limit-rate 200k https://google.com

-o (Save to File)

Saves the response body to a specified file (similar to wget):

$ curl -o example.html https://www.example.com

-O (Save with Remote Name)

Saves the response using the remote filename:

$ curl -O https://www.example.com/foo/bar.html

-s (Silent)

Suppresses progress and error messages. Combine with -o /dev/null for completely silent execution:

$ curl -s https://www.example.com
$ curl -s -o /dev/null https://google.com

-S (Show Errors)

Shows only error messages; often used together with -s:

$ curl -S -s -o /dev/null https://google.com

-u (User Authentication)

Provides basic authentication credentials: $ curl -u 'bob:12345' https://google.com/login Credentials can also be embedded in the URL:

$ curl https://bob:[email protected]/login

-v (Verbose)

Outputs the full request/response exchange for debugging:

$ curl -v https://www.example.com
--trace -

prints raw binary data as well:

$ curl --trace - https://www.example.com

-x (Proxy)

Specifies a proxy server. If no scheme is given, HTTP is assumed:

$ curl -x socks5://james:[email protected]:8080 https://www.example.com
$ curl -x james:[email protected]:8080 https://www.example.com

-X (Custom Method)

Overrides the HTTP method used for the request:

$ curl -X POST https://www.example.com
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HTTPcURLtool
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

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.