Writing a Response
Lesson 24 of 47 in Coddy's Networking with the Terminal course.
You have read plenty of responses. Now write one.
nc -l listens on a port, and whatever you feed its input is sent to the first client that connects. That makes you the server, with no web server involved at all:
printf 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhand written' | nc -l -p 9114 &Look at what that is: a status line, one header, a blank line, then the body. Exactly the shape you have been reading, typed out by hand.
Now anything that speaks HTTP will accept it. Point curl at the port and it reports a perfectly ordinary 200 with a text/plain body, because that is all a web server ever does: write text in this shape.
This trick is genuinely useful at work. When you need to see what something is sending you, put an nc -l in its way and read the request it makes.
Challenge
ExpertBe the server, by hand.
Using nc -l on port 9114, serve a response whose status line is HTTP/1.1 200 OK, with a Content-Type: text/plain header and a body of exactly hand written. Then fetch it with curl and print the body.
Expected output:
hand writtenUse
printfwith\r\nline endings, and remember the blank line before the body.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Networking with the Terminal
5HTTP by Hand
Anatomy of a RequestRequest MethodsStatus CodesResponse HeadersWriting a ResponseRecap: HTTP BasicsPractice on your own: Terminal playground