When working with web servers or troubleshooting HTTP requests, it can be handy to retrieve only the HTTP status code from a response. This can be helpful for testing the availability of an API endpoint or monitoring the health of a website. This tutorial explains how to print only HTTP status code with wget.
The wget command does not have a built-in option to print only the HTTP status code. To achieve this, we need to combine it with other commands, such as awk, to extract the code.
wget -S -qO /dev/null https://httpbin.org/get 2>&1 | awk '/HTTP\/[0-9.]+/{print $2}'
Explanation:
-S
- displays the server response headers, including the HTTP status line.-q
- it stands for "quiet", allowing the command to run silently without showing download progress.-O /dev/null
- redirects the downloaded content to/dev/null
, ensuring the body of the response is not saved.2>&1
- it redirects the stderr (where the headers are written) to stdout, allowing us to process all output in the next step./HTTP\/[0-9.]+/
- matches lines containing the HTTP version (e.g.,HTTP/1.1
orHTTP/2
).{print $2}
- extracts and prints the second field of the matching line, which is the HTTP status code (e.g.,200
,404
).
Leave a Comment
Cancel reply