Testing your REST-services in the commandline
- Written by Dave on Saturday 30 July 2011
Developing a REST API often requires requests to be accessible with a specific request method; GET to retrieve data, POST to send data and DELETE to indicate a removal of data. If there is time you could write a client with a basic gui to accomplish this, or you could test it by using the commandline and cURL. I've made a small alias method to do just that:
curlrest()
{
method=$1
url=$2
parameters=$3
if [[ "$method" == "" ]] || [[ "$url" == "" ]]
then
echo "usage: curlrest <method> <url> <*parameters>"
else
if [[ "$parameters" != "" ]]
then
parameters="--data-urlencode $(echo $parameters | xargs | sed 's/&/ --data-urlencode /g')"
fi
curl -v --globoff --get -X$method $url $parameters
echo ""
fi
}
The key here is the combination of the --get and -X parameters of cURL. It allows you to overwrite the request method with whatever string you like, and send any additional parameters along as GET data.
I realise this is cheating and technically not a correct way to do it (+ sending files with PUT is not testable this way), but I find it an effective way to quickly test API responses to any other request during development.