$_ CLI In Depth
Learning progress 0%
lesson networking sockets ssh

HTTP from the Shell with curl

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:17

The shape of a request

An HTTP/1.1 request starts with a request line that names the method, the path, and the version. The headers follow, one per line, and a blank line ends the headers. Lines end with a carriage return and a line feed, written as \r\n. The Host header is required in HTTP/1.1, because one server can host many sites.

The raw request for a page is the text below. Each line is sent exactly as written, and the blank line at the end is what tells the server the request is complete.

TEXT1 lines
GET /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: manual\r\nConnection: close\r\n\r\n

The shape of a response

The response starts with a status line, such as HTTP/1.1 200 OK. The three-digit code is what programs check. Codes in the 200 range mean success, 300 range mean redirection, 400 range mean the client asked for something the server will not give, and 500 range mean the server failed. The reason phrase after the code is for people, and programs should not depend on it.

TEXT1 lines
HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nnot here\r\n

This sample response means the path does not exist. The status line is the first line of the output, and the second field, 404, is the value a script should test.

Reading the status code in a shell script

To get the code from a saved response, take the first line and print the second field. The head command reads the first line, and awk prints the field. This works on any response file you have, including one that curl saved.

BASH1 lines
head -1 response.txt | awk '{print $2}'

For the sample above, the command prints 404. A script can compare that value and decide what to do, which is the first step in checking a service in a health probe.

What curl adds

The curl tool performs the same steps with a real connection. Its common flags are -s for silent output, -i to include the headers, -o to write the body to a file, and -I to send a HEAD request that returns only the headers. A typical call looks like the command below, which is shown as reference text because curl is not part of this lab image and the lab machine has no network.

TEXT1 lines
curl -i https://example.com/
Note

Run curl on your own machine when you want to reach real websites. Outside this lab, the commands in this unit work as written, but the lab image has no network and no curl.

Local tests with bash

When no HTTP client is available, the raw request can be sent through /dev/tcp to a server on the same machine. The server must already be listening, and the response comes back on the same connection. The lab image has no HTTP server, so this unit only shows the pattern: open the descriptor, write the request, and read the reply.

TEXT3 lines
exec 3<>/dev/tcp/127.0.0.1/8080
printf "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" >&3
cat <&3

Hands-on: keep the status code

The lab gives you a saved response with a 404 status. Your task is to extract the status code with the pipeline from this unit and write it to a file. The same pipeline works on any saved response, and on real responses from curl with the -i flag.

Terminal lab · 2 steps · real Linux in your browser

Read a status code and write a request

Pull the status code out of a saved response and write a raw HTTP request with CRLF line endings.

01
Extract the status code
The file response.txt in /workspace holds a saved HTTP response. Write its status code to status.txt, using head to take the first line and awk to print the second field: ``bash head -1 response.txt | awk '{print $2}' > status.txt ``
Show hint for step 1
The first line is the status line. The second whitespace-separated field is the code.
02
Write a raw HTTP request
Create request.txt in /workspace containing this HTTP/1.1 request. Every line ends with CRLF, and a blank line ends the headers. Use printf with \r\n escapes: ``bash printf 'GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n' > request.txt `` Count the carriage returns to check your work. There should be four.
Show hint for step 2
Each escape \r is one carriage return. The request has a request line, two headers, and a blank line, so four lines end with CRLF.
Check yourself Question 1 of 4
Concept Check Where the status code lives recall

In an HTTP response, which line carries the three-digit status code?

Recap & Key Takeaways

An HTTP request is plain text with a request line, headers, and a blank line, and lines end with CRLF.
The status line of a response carries the three-digit code, and scripts should test the code rather than the reason phrase.
head -1 with awk on the status line extracts the code from any saved response without an HTTP client.
curl is a reference tool here: the lab image has no curl and no network, so use the raw-text pattern to practice.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue