Last Updated: September 27, 2021
·
88.08K
· mrallen1

Use HTTP status codes from curl

You can make curl return actual HTTP status codes on standard out as long as you use the

-w <format> or --write-out <format>

command line option, using the format of %{http_code}

This gives you an easy way to poll an API endpoint using something as simple as bash without having to look up curl's exit code meanings:

#!/bin/bash

while true
do
  STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://example.com/poll-me)
  if [ $STATUS -eq 200 ]; then
    echo "Got 200! All done!"
    break
  else
    echo "Got $STATUS :( Not done yet..."
  fi
  sleep 10
done

You can learn more output formats by consulting the curl man page and searching for write-out in the page content.