Guides / DevOps / The curl Command: 15 Examples You’ll…
DevOps · Guide

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.

opnF.shNov 22, 20266 min readcurl commandcurlapi
On this page 4 sections ▾

TL;DR

  • Plain curl URL gets a page. Most other examples add one flag: -o to save, -L to follow redirects, -d or --json to send data.
  • Add -f in scripts. Without it, curl treats a 404 as success.
  • When something's wrong, -v shows 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.

i
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.

one GET request
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.

-o picks the name, -O keeps it
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.

a HEAD request
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.

status, headers, then 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: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.

without -L, then with it
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.

-X, -H and -d together
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.

the same request with one flag
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.

form fields, 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.

a DELETE request
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.

turned away, then let in
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.

basic authentication
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.

save the cookie, send it back
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.

a 200, then a 404
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.

exit code 22
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.

> sent, < received
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
i
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.

Open ›
How do I send a POST request with curl?
Use --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?
Put an @ 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?
Both can download files. wget is built mainly for downloading, including whole websites with its recursive mode. curl is built for talking to servers in general: any HTTP method, custom headers, logins, and protocols beyond HTTP like FTP.
How do I download a file with curl?
Use 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.

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.