taqiyg
Last Updated: December 19, 2016
·
17.96K
· mrallen1
7330b65ea54e1b90a987a552e5c5cf32

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.

Say Thanks
Respond
Filed Under