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.
GET /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: manual\r\nConnection: close\r\n\r\nThe 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.
HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nnot here\r\nThis 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.
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.
curl -i https://example.com/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.
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 <&3Hands-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.