Testing REST API Calls using Curl

Representational State Transfer (REST) was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation. Fielding is one of the principal authors of the Hyper Text Transfer Protocol (HTTP) specification versions 1.0 and 1.1.

Since my current application is just a bunch of REST services, I needed to test these calls before publishing them. Unfortunately there were no good tools I could use to test these calls. I spent some time searching for scripts or apps that can make my life easier till I ran into this jewel somewhere on the web

You can just copy and paste this code into a file called apitest.sh and your good to go.

#!/bin/bash
AUTH="user:password"
BASE="http://example.com/"
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

1 comment

Jitendra's picture

Good stuff...did not know

Submitted by Jitendra (not verified) on Sun, 02/21/2010 - 03:23.

Good stuff...did not know that you could execute CURL from the shell directly. Indeed good way to test.

-Jitendra

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>, <apache>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Drupal theme by Kiwi Themes.