Bootstrap FreeKB - Curl - Display HTTP return code using the -w or --write-out "%{http_code}" option
Curl - Display HTTP return code using the -w or --write-out "%{http_code}" option

Updated:   |  Curl articles

The curl command with the -w or --write-out option followed by "%{http_code}" can be used to display the HTTP return code of the request. In this example, the request to get the index.html page from the www.example.com web server returned HTTP code 200 (OK).

~]# curl --write-out "%{http_code}" http://www.example.com/index.html
<html>
  <body>
    Hello World
  </body>
</html>
200

 

You may want to also use the -s or --silent flag and the -o /dev/null option to suppress the response body output. Now, only the HTTP response code is returned.

~]# curl --silent --output /dev/null --write-out "%{http_code}" http://www.example.com/index.html
200

 

In this example, 404 (not found) is returned when attempting to get foo.html from the www.example.com web server, because there is no such file as foo.html on the www.example.com web server.

~]# curl --silent --output /dev/null --write-out "%{http_code}" http://www.example.com/foo.html
404



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e41901 in the box below so that we can be sure you are a human.