HTTP from the Terminal: curl and wget
By the end of this lesson you'll make HTTP requests straight from the shell: GET and POST with curl, send headers and bodies, authenticate, read status codes, pipe responses into other tools, and download or mirror files with wget — plus a clear sense of when to reach for each tool.
Learn HTTP from the Terminal: curl and wget in our free Command Line course — an interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Cli course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
1. curl Basics: GET and Saving the Response
A plain curl URL sends a GET and prints the response body to standard output. To save it instead, use -o name to choose your own filename or -O to reuse the remote filename. Add -L so curl follows redirects (301/302) to the final page rather than printing the redirect itself.
2. Response Headers, Request Headers, and Silent Mode
Use -i to include the response headers (status line, Content-Type, and so on) above the body. To send your own request headers, repeat -H 'Header: value' . In scripts, add -s (silent) to hide the progress meter so only the real output is printed.
3. Sending Data: POST, -d, and JSON
-d 'key=value' sends a request body ; using -d implies POST and sets Content-Type: application/x-www-form-urlencoded for you. For a JSON API, put JSON in -d and declare it with -H 'Content-Type: application/json' . Use -X to pick another method explicitly, such as PUT or DELETE .
4. Authentication: Basic Auth and Bearer Tokens
For HTTP basic auth , pass -u user:pass and curl builds the Authorization header for you. Most modern APIs use a Bearer token instead, which you send as a header: -H "Authorization: Bearer TOKEN" . Either approach combines naturally with -X , -H , and -d for authenticated requests.
5. Status Codes and Piping the Response
To print just the status code , silence curl, discard the body to /dev/null , and ask for it with the write-out option: curl -s -o /dev/null -w "%{' http_code '}" URL . Because curl prints the body to stdout , you can pipe it straight into tools like grep — or into a JSON parser, which is the focus of the next lesson.
6. wget: Downloading, Resuming, and Mirroring
wget is built for downloads: it saves to disk by default using the remote filename. Add -c to resume a partial download after a dropped connection. To mirror part of a site, use -r (recursive) with -np (no-parent, stay in the starting folder) and -l to limit the depth.
Fill in the one blank marked ___ using the # 👉 hint so curl follows redirects and saves the page, then run it and check the Output panel matches.
Make curl print just the HTTP status code by filling in the write-out variable. Fill in the blank, then run it.
No blanks this time — just a brief and an outline. Combine a status-code check, a download with curl and wget, a JSON POST, and a resumed wget into a few commands that demonstrate everything from this lesson, and check your output against the hints in the comments.
Practice quiz
What does curl print by default when you run curl https://example.com ?
- The response body to standard output
- Nothing unless you pass -o
- The response saved to a file named example.com
- Only the HTTP status code
Answer: The response body to standard output. curl writes the response body to stdout by default, which is exactly why it pipes so nicely into tools like grep or a JSON parser.
Which curl flag tells it to follow HTTP redirects such as 301 and 302?
- -r
- -f
- -L
- -R
Answer: -L. -L (location) makes curl follow redirects to the final URL. Without it curl just prints the redirect response and stops.
What is the difference between curl -o name.html URL and curl -O URL?
- They are identical
- -o saves to a filename you choose; -O saves using the remote filename
- -o follows redirects; -O does not
- -O saves to a filename you choose; -o uses the remote filename
Answer: -o saves to a filename you choose; -O saves using the remote filename. Lowercase -o lets you name the output file. Uppercase -O reuses the last path segment of the URL as the filename.
What does curl -d 'name=ada' https://api.example.com/users do?
- Downloads the page to a file
- Sends a GET request with a query string
- Deletes the resource
- Sends a POST request with that data as the body
Answer: Sends a POST request with that data as the body. Using -d sends a request body and implies POST, with Content-Type set to application/x-www-form-urlencoded unless you override it.
Which flag adds a request header in curl?
- -H 'Content-Type: application/json'
- -A 'Content-Type: application/json'
- -D 'Content-Type: application/json'
- -X 'Content-Type: application/json'
Answer: -H 'Content-Type: application/json'. -H (header) adds a single request header. Repeat -H to send several headers such as Content-Type and Authorization.
What does the -s flag do in curl?
- Shows the response headers
- Silences the progress meter and error messages
- Saves the body to a file
- Sends the request securely over TLS
Answer: Silences the progress meter and error messages. -s (silent) hides the progress meter and error text, which is handy in scripts so only the real output is printed.
Which curl flag includes the response headers in the printed output along with the body?
- -H
- -h
- -v
- -i
Answer: -i. -i (include) prints the response status line and headers before the body. -H, by contrast, sends a request header.
How do you resume an interrupted download with wget?
- wget -O URL
- wget -L URL
- wget -c URL
- wget -r URL
Answer: wget -c URL. wget -c (continue) resumes a partially downloaded file from where it stopped instead of starting over.
Which curl flag sets a username and password for HTTP basic authentication?
- -b user:pass
- -u user:pass
- -p user:pass
- -a user:pass
Answer: -u user:pass. -u user:pass sends HTTP basic auth credentials. For token APIs you instead send a header like -H 'Authorization: Bearer TOKEN'.
Which tool is built primarily for downloading files and recursively mirroring sites, saving to disk by default?
- wget
- grep
- ssh
- curl
Answer: wget. wget saves to disk by default and supports recursive mirroring with -r. curl is a flexible single-request transfer tool that prints to stdout and pipes well.