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 customize headers, handle cookies, send data, manage redirects, control output, and debug requests for efficient web and API interactions.
简介
curl is a widely used command‑line tool for making HTTP requests; its name stands for "client URL".
It offers powerful functionality with dozens of options, and when mastered it can replace graphical tools like Postman.
This article lists the most common curl options as a quick reference, based on the "curl cookbook". Example outputs are omitted for brevity.
Without any arguments, curl performs a GET request.
$ curl https://www.example.com-A
The -A option sets the User‑Agent header. By default curl uses curl/[version].
$ 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.comUse an empty string to remove the User‑Agent header: $ curl -A '' https://google.com You can also set the header directly with -H:
$ curl -H 'User-Agent: php/1.0' https://google.com-b
The -b option sends cookies to the server.
$ curl -b 'foo=bar' https://google.com $ curl -b 'foo1=bar;foo2=bar2' https://google.comRead cookies from a file:
$ curl -b cookies.txt https://www.google.com-c
The -c option writes received cookies to a file.
$ curl -c cookies.txt https://www.google.com-d
The -d option sends data in the request body, automatically switching to POST and adding the Content-Type: application/x-www-form-urlencoded header.
$ curl -d 'login=emma&password=123' -X POST https://google.com/login
# or
$ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/loginYou can read data from a file using @:
$ curl -d '@data.txt' https://google.com/login--data-urlencode
This option works like -d but URL‑encodes the data automatically.
$ curl --data-urlencode 'comment=hello world' https://google.com/login-e
Set the HTTP Referer header.
$ curl -e 'https://google.com?q=example' https://www.example.comOr use -H directly:
curl -H 'Referer: https://google.com?q=example' https://www.example.com-F
The -F option uploads files as multipart/form‑data.
$ curl -F '[email protected]' https://google.com/profileSpecify MIME type or filename:
$ curl -F '[email protected];type=image/png' https://google.com/profile
$ curl -F '[email protected];filename=me.png' https://google.com/profile-G
Use -G to send data as a query string in a GET request.
$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/searchCombine with --data-urlencode for URL‑encoded parameters.
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com-H
Add arbitrary HTTP headers.
$ curl -H 'Accept-Language: en-US' https://google.com
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.comSend JSON data with a custom Content-Type header:
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login-i
Print response headers followed by the body.
$ curl -i https://www.example.com-I
Send a HEAD request and display only the response headers.
$ curl -I https://www.example.com
# equivalent to --head
$ curl --head https://www.example.com-k
Skip SSL certificate verification.
$ curl -k https://www.example.com-L
Follow HTTP redirects.
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet--limit-rate
Throttle bandwidth to simulate slow connections.
$ curl --limit-rate 200k https://google.com-o
Save the response body to a file (similar to wget).
$ curl -o example.html https://www.example.com-O
Save the response using the remote filename.
$ curl -O https://www.example.com/foo/bar.html-s
Silent mode: suppress progress and error messages. $ curl -s https://www.example.com Combine with -o /dev/null to discard all output unless an error occurs:
$ curl -s -o /dev/null https://google.com-S
Show errors only, typically used with -s.
$ curl -S -s -o /dev/null https://google.com-u
Provide username and password for HTTP authentication. $ curl -u 'bob:12345' https://google.com/login Credentials can also be embedded in the URL:
$ curl https://bob:[email protected]/login-v
Verbose mode prints the full request/response exchange for debugging. $ curl -v https://www.example.com The --trace option adds raw binary traces.
$ curl --trace - https://www.example.com-x
Specify a proxy for the request.
$ curl -x socks5://james:[email protected]:8080 https://www.example.com
# default protocol is HTTP if omitted
$ curl -x james:[email protected]:8080 https://www.example.com-X
Explicitly set the HTTP method.
$ curl -X POST https://www.example.comSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
