The curl command
The curl command fetches a URL and prints what comes back…
The curl command fetches a URL and prints what comes back, which makes it the quickest way to test a website or an API from the terminal.
Taught in Intermediate, lesson 30; more in Advanced and Ninja
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
curl [-I] [-i] [-L] [-s] [-o FILE] [-w FORMAT] URL...
Fetch a URL, or test a web server. Fetches a web address and prints what comes back. It is the first tool for "is the website up?": curl -I shows just the status line and headers (200 OK, 301 Moved, 404 Not Found, 502 Bad Gateway). Its errors tell you where things broke: "Could not resolve host" is DNS, "Couldn’t connect to server" right away means nothing listens on that port (connection refused), and a long wait that ends in a timeout usually means a firewall drops the packets. Try http://web/, http://web/api/ and http://example.com/. In this practice shell the network is a simulation inside your browser: the machines web, db and router and the website example.com are pretend, and nothing leaves your device.
In the practice terminal:A simulation: web, db, router and example.com are pretend machines inside your browser, and nothing leaves your device. curl here reaches only the pretend servers, never real websites.
How to use curl
Fetch a web page
curl prints the page as the server sent it, here HTML.
curl http://web/<!DOCTYPE html> <html> <head><title>Shop</title></head> <body> <h1>Welcome to the shop</h1> <p>Served by nginx on web (a pretend server in your browser).</p> </body> </html>
Only the headers: curl -I
The first line is the status: 200 OK, 301 a redirect, 404 not found, 500 or 502 a problem on the server.
curl -I http://example.com/HTTP/1.1 200 OK Server: ECS (pretend) Date: Mon, 12 Jan 2026 09:01:00 GMT Content-Type: text/html Content-Length: 210 Connection: keep-alive Cache-Control: max-age=604800
Follow redirects: curl -L
A 301 or 302 answer points somewhere else in its Location header, and -L follows it.
curl -I http://www.example.com/HTTP/1.1 301 Moved Permanently Server: ECS (pretend) Date: Mon, 12 Jan 2026 09:01:00 GMT Content-Type: text/html Content-Length: 123 Connection: keep-alive Location: https://www.example.com/
curl -L http://www.example.com/<!DOCTYPE html> <html> <head><title>Example Domain</title></head> <body> <h1>Example Domain</h1> <p>This is a pretend website inside the practice shell. Nothing here came from the internet.</p> </body> </html>
Save to a file: curl -o and -O
-o name saves under a name you choose and -O under the name in the URL. The progress meter goes to the error output; -s hides it.
curl -o page.html http://example.com/ && ls -l page.html % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 210 100 210 0 0 1k 0 --:--:-- --:--:-- --:--:-- 1k
-rw-r--r-- 1 learner learner 210 Jan 12 09:01 page.html
Just the status code
-w prints facts about the request. With -s -o /dev/null that is all you see, which suits scripts and health checks.
curl -s -o /dev/null -w "%{http_code}\n" http://web/api/200
See the whole conversation: curl -v
Lines starting with > are what curl sent and < what came back.
curl -v http://web/* Host web:80 was resolved. * Trying 10.0.0.10:80... * Connected to web (10.0.0.10) port 80 > GET / HTTP/1.1 > Host: web > User-Agent: curl/8.5.0 > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.24.0 (Ubuntu) < Date: Mon, 12 Jan 2026 09:01:00 GMT < Content-Type: text/html … and 15 more lines
Options
| -I | headers only (a HEAD request) |
|---|---|
| -i | headers, then the page |
| -L | follow redirects |
| -s | silent: no progress or errors |
| -o FILE | save to a file |
| -O | save under the name in the URL |
| -w FORMAT | print details after, such as %{http_code} |
| -v | show the conversation with the server |
| --connect-timeout N | give up connecting after N seconds |
Questions about curl
What is the difference between curl and wget?
curl prints to the terminal by default and handles many kinds of request, which suits testing and APIs. wget saves to a file and can download a whole site. Both fetch URLs.
How do I send data with curl?
Use -d, which sends a POST: curl -d "name=Ada" URL. For JSON add -H "Content-Type: application/json". The pretend servers here answer every request the same way.
What does "Failed to connect ... Connection refused" mean?
The machine answered, but nothing listens on that port: the web server is stopped or uses another port. A timeout instead points to a firewall.
Why does curl print a table of numbers?
That is the progress meter, shown when the output goes to a file. Add -s to hide it.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.