The curl Command: 15 Examples You’ll Actually Use
The curl command explained through 15 examples: download files, follow redirects, send JSON and forms, log in with tokens and cookies, and debug requests.

On this page 4 sections ▾
TL;DR
- Plain
curl URLgets a page. Most other examples add one flag:-oto save,-Lto follow redirects,-dor--jsonto send data. - Add
-fin scripts. Without it, curl treats a 404 as success. - When something's wrong,
-vshows exactly what curl sent and what came back.
Open a terminal, type one line, and you can fetch a page, download a file, send JSON to an API, log in, or find out exactly why a request is failing. These 15 examples of the curl command are the ones worth keeping close, each shown with what it really prints.
Quick facts
-O overwrites any file in your folder with the same name, without asking first. Add --no-clobber and curl saves under a new name instead.
When -L follows a redirect to a different website, curl leaves your password, cookies and Authorization header behind instead of sending them along. Other custom headers do get passed on.
--json doesn't check that what you send is valid JSON. It sends whatever you type.
About the examples
Every command below was run against a small test API on localhost:8000, so you can see the real output. Swap in your own URL; on a public site you'll see different headers and an https:// address.
#Fetch pages and files with the curl command
1. Get a page or API response
With nothing but a URL, curl sends a GET request and prints whatever comes back.
student@opnf:~$ curl http://localhost:8000/users/1 { "id": 1, "name": "Ana", "city": "Austin" }
2. Save it to a file
-o saves the response under the name you choose. -O keeps the file's own name from the URL. Once the output isn't going to your screen, curl shows a progress meter while it downloads.
student@opnf:~$ curl -o sales.csv http://localhost:8000/report.csv % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 30 100 30 0 0 30395 0 --:--:-- --:--:-- --:--:-- 30000 student@opnf:~$ curl -O http://localhost:8000/report.csv % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 30 100 30 0 0 26929 0 --:--:-- --:--:-- --:--:-- 30000 student@opnf:~$ ls report.csv sales.csv
3. Check the headers only
-I asks for just the headers, which tells you the status, the content type and the size without downloading the body.
student@opnf:~$ curl -I http://localhost:8000/users/1 HTTP/1.1 200 OK Server: DemoAPI/1.0 Date: Sun, 08 Nov 2026 02:10:34 GMT Content-Type: application/json Content-Length: 51
4. See headers and body together
-i prints the headers first, then the body, all in one go.
student@opnf:~$ curl -i http://localhost:8000/users/1 HTTP/1.1 200 OK Server: DemoAPI/1.0 Date: Sun, 08 Nov 2026 02:10:35 GMT Content-Type: application/json Content-Length: 51 { "id": 1, "name": "Ana", "city": "Austin" }
5. Follow redirects
Many URLs answer with "moved, go here instead". Without -L, curl stops at that message. With it, curl goes to the new address and fetches the real thing.
student@opnf:~$ curl http://localhost:8000/old { "moved": "/users/1" } student@opnf:~$ curl -L http://localhost:8000/old { "id": 1, "name": "Ana", "city": "Austin" }
#Send data
6. POST JSON the long way
-X POST picks the method, -H adds a header saying the body is JSON, and -d is the body itself. You'll see -X POST in a lot of API docs, though it's optional here: example 8 shows why.
student@opnf:~$ curl -X POST -H "Content-Type: application/json" -d '{"name":"Ben"}' http://localhost:8000/echo { "method": "POST", "content_type": "application/json", "body": "{\"name\":\"Ben\"}" }
7. POST JSON the short way
--json does all three in one go. It also adds a header asking for JSON back, and it's been in curl since version 7.82.0.
student@opnf:~$ curl --json '{"name":"Ben"}' http://localhost:8000/echo { "method": "POST", "content_type": "application/json", "body": "{\"name\":\"Ben\"}" }
8. Send a form
-d on its own sends the data the way a web form does, as a POST with the form content type.
student@opnf:~$ curl -d "name=Ben&city=Boston" http://localhost:8000/echo { "method": "POST", "content_type": "application/x-www-form-urlencoded", "body": "name=Ben&city=Boston" }
9. Use other methods
-X sets any HTTP method, like PUT, PATCH or DELETE.
student@opnf:~$ curl -X DELETE http://localhost:8000/echo { "method": "DELETE", "content_type": null, "body": "" }
#Log in
10. Send a token or custom header
-H adds any header you like. Many APIs want a token in an Authorization header, and you get a 401 error without one.
student@opnf:~$ curl http://localhost:8000/private { "error": "unauthorized" } student@opnf:~$ curl -H "Authorization: Bearer abc123" http://localhost:8000/private { "secret": "you are in" }
11. Log in with a username and password
-u sends a username and password using basic authentication. Leave out the password (-u ana) and curl asks for it instead.
student@opnf:~$ curl -u ana:pa55 http://localhost:8000/private { "secret": "you are in" }
Where it breaks
A password typed on the command line can end up in your shell history and, for a moment, be visible to other users on the same machine. For anything real, use -u ana and let curl prompt you, or keep it in a .netrc file and add --netrc.
12. Keep a session with cookies
-c saves the cookies a site sets, and -b sends them back next time, which is how you stay logged in across requests.
student@opnf:~$ curl -c cookies.txt http://localhost:8000/login { "ok": true } student@opnf:~$ curl -b cookies.txt http://localhost:8000/me { "session": "session=xyz789" }
#Script and debug
13. Print just the status code
-s hides the progress meter, -o /dev/null throws the body away, and -w prints whatever you ask for after the request, here the HTTP status code.
student@opnf:~$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/users/1 200 student@opnf:~$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/nope 404
14. Fail when the server returns an error
-f makes curl stop with exit code 22 when the server answers with a 400 or 500 error, so a script can notice something went wrong. It isn't watertight: the manual warns that some login errors (401 and 407) can slip through.
student@opnf:~$ curl -f http://localhost:8000/nope curl: (22) The requested URL returned error: 404 student@opnf:~$ echo $? 22
15. See everything that happens
-v shows the whole conversation: the connection, every header curl sends (lines starting >) and every header it gets back (lines starting <). It's the first thing to try when a request isn't doing what you expect.
student@opnf:~$ curl -v http://localhost:8000/users/1 * Host localhost:8000 was resolved. * IPv6: ::1 * IPv4: 127.0.0.1 * Trying 127.0.0.1:8000... * Connected to localhost (127.0.0.1) port 8000 > GET /users/1 HTTP/1.1 > Host: localhost:8000 > User-Agent: curl/8.5.0 > Accept: */* > < HTTP/1.1 200 OK < Server: DemoAPI/1.0 < Date: Sun, 08 Nov 2026 02:10:53 GMT < Content-Type: application/json < Content-Length: 51 < { "id": 1, "name": "Ana", "city": "Austin" } * Connection #0 to host localhost left intact
Careful with -k
You'll see -k suggested online to get past certificate errors. It turns off the check that you're really talking to the site you asked for, so use it only on your own test servers, never to make a real site's error go away.

Do it hands-on
Linux Fundamentals, Level 1 of DevOps Pro, is where you actually work with commands like these, step by step, with labs and quizzes in a simulated terminal. Level 1 is free.
How do I send a POST request with curl?
--json for JSON (example 7) or -d for form fields (example 8). To post a file's contents instead, put @ before its name, as in the next answer.How do I send JSON from a file with curl?
@ before the file name: curl --json @new-user.json URL reads the body from new-user.json, which beats squeezing long JSON onto one line.What's the difference between curl and wget?
How do I download a file with curl?
curl -O URL to keep the file's own name, or curl -o name URL to pick one. Both save into the folder you're in; add --output-dir to save somewhere else.Sourcescurl manual
Get opnF in your inbox
Tech news for IT professionals that actually teaches you something.
Our promise to you: we'll be conscious about email quality/frequency, and your address will never be shared/sold.