Diagnosing HTTP With curl
Goal
You pull status codes, headers, redirects, virtual hosts, and timing with curl, and practice turning "it's slow" and "it doesn't work" into segment names.
Why it matters
The timing fields of curl -w break one request into five segments: name resolution / TCP connect / TLS / first byte / total. With this breakdown, you can turn the report "the API is slow" into "the TLS handshake is 800ms", and at that moment the responsible team and the action are decided. --resolve and -H 'Host:' are essential tools for pre-deployment verification — you can test a new server by its real name before changing DNS, and while knocking directly on a backend behind an LB, you can still pass through virtual host routing as originally.
Steps
- Create the
/root/httpdirectory and start/opt/fixtures/nt-http/server.pyon port 8088 in the background. It must still be alive at grading time. - Request
http://127.0.0.1:8088/okin verbose mode and save the full output, including the request and response headers, to/root/http/verbose.txt. - Write the status codes of the four paths
/ok,/notfound,/teapot, and/redirect, one line each in the format경로 코드(path, then code), in/root/http/status.txt. (Do not follow the redirect) - Save only the response headers of
/okto/root/http/headers.txt. TheX-Labhub-Nodeheader must be included. - Request
/chainfollowing redirects, and write the final status code and the number of redirects on one line,code=<코드> redirects=<횟수>(code= followed by the code and redirects= followed by the count), in/root/http/redirect.txt. - Request
/whoamiwith the headerHost: shop.labhub.localattached and save the response body to/root/http/host.txt. - Use
--resolveto makehttp://shop.labhub.local:8088/oksucceed without touching/etc/hosts, and save that response body to/root/http/resolve.txt. - Make
/root/http/timing.txtwith the following 4 lines. The values are the actual measurements obtained by requesting the/slowpath.code=<상태코드>/connect=<time_connect>/ttfb=<time_starttransfer>/total=<time_total>(each followed by the measured value)/slowhas a 1.2-second delay, sototalmust be at least 1.
Notes
- You pull only the code with
curl -o /dev/null -s -w '%{http_code}\n' <URL>. - To see only the headers,
curl -sD - -o /dev/null <URL>is safe.-Isends HEAD, so the result may differ depending on the server implementation. - The format is
--resolve <이름>:<포트>:<IP>(name, port, IP). - Common mistake 1: if you follow the redirect in step 3,
/redirectcomes out as 200. Leave out-L. - Common mistake 2: in step 2, the
-voutput is standard error, so it is not saved with>alone.
Starting the diagnostic server
Create the /root/http directory and start /opt/fixtures/nt-http/server.py on port 8088 in the background. It must still be alive at grading time.
The fixture server takes the port as an argument. After starting it, check the listening with ss.
Viewing the full request and response headers
Request http://127.0.0.1:8088/ok in verbose mode and save the full output, including the request and response headers, to /root/http/verbose.txt.
curl's verbose mode comes out on standard error. To keep it in a file, you need to use redirection as well.
Status code table per path
Write the status codes of the four paths /ok, /notfound, /teapot, and /redirect, one line each in the format 경로 코드 (path, then code), in /root/http/status.txt. (Do not follow the redirect)
You can pull out only the status code with an output format specifier. Discard the body and keep only the code.
Pulling out only the response headers
Save only the response headers of /ok to /root/http/headers.txt. The X-Labhub-Node header must be included.
If you combine the option that dumps the headers to standard output and the option that discards the body, you can see only the headers while keeping GET.
Following redirects
Request /chain following redirects, and write the final status code and the number of redirects on one line, code=<코드> redirects=<횟수> (code= followed by the code and redirects= followed by the count), in /root/http/redirect.txt.
There is an option that follows redirects, and an output variable that tells you how many times it followed.
Testing a virtual host with the Host header
Request /whoami with the header Host: shop.labhub.local attached and save the response body to /root/http/host.txt.
The fixture server's /whoami returns the Host header it received as it is.
Connecting by name without DNS
Use --resolve to make http://shop.labhub.local:8088/ok succeed without touching /etc/hosts, and save that response body to /root/http/resolve.txt.
--resolve takes the three values name:port:address joined by colons. You can do it without touching /etc/hosts.
Timing breakdown and report
Make /root/http/timing.txt with the following 4 lines. The values are the actual measurements obtained by requesting the /slow path.
code=<상태코드> / connect=<time_connect> / ttfb=<time_starttransfer> / total=<time_total> (each followed by the measured value)
/slow has a 1.2-second delay, so total must be at least 1.
The timing fields are cumulative values. The /slow path deliberately responds slowly.