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.
Written by Mark Allen
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Curl
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#