DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

REST Curl Helper

02.13.2007
| 7781 views |
  • submit to reddit
        // A small bash script that wraps around curl to help more easily test RESTful sites.

#!/bin/bash
AUTH="user:password"
BASE="http://localhost:3000"
METHOD=$1
DEST="$BASE$2"
XML=$3

# make sure args were passed
if [ $# -eq 0 ]; then
        echo "usage: ./`basename $0` HTTP-METHOD DESTINATION_URI [XML]"
        echo "example: ./`basename $0` POST "/accounts" \"<account><name>ed</name><email>ed@ed.com</email></account>\""
        exit 1
fi

# execute CURL call
curl -H 'Accept: application/xml' -H 'Content-Type: application/xml' -w '\nHTTP STATUS: %{http_code}\nTIME: %{time_total}\n' \
-X $METHOD \
-d "$XML" \
-u "$AUTH" \
"$DEST"

exit 0