Testing APIs

I’ve been doing a lot of web API testing lately.  There are two main flavors of web APIs:  SOAP and REST.

SOAP is verbose, ugly, and generally awful to work with.  In order to work with SOAP APIs first, you need to have a SOAP library for your language of choice.  Then you have to generate a “driver” specific to the API you are working with.  Next, you have to figure out the commands that are available, and finally, you have to parse though the xml that the calls to the API return.

REST, on the other hand, is simple.  Call a url with query parameters like you would a normal web page.  Get a result in xml, json, or whatever other format the API supports.  Parse it.  Done.  The main benefit is that testing is much easier.  Fire up your friendly neighborhood web browser and paste in the url and look at the xml returned.

But not so fast!  That’s all well and good for reading an API, but what about sending data?  For that, you can sometimes use a web browser too, but not always.  Most REST APIs require POSTs to create objects.  For that, you need a bit more.  If you like GUIs, one option is a Firefox plugin called REST Client that I’ve found works great.  For the command-line there’s cURL.  Here are a couple examples of how to use cURL.

# If the API requires authentication
curl -u user:pass -F "file=@filename_to_send" http://www.example.com/api

# If the API requires you to set the content type and send raw data
curl -H "Content-Type:text/xml" -d @filename_to_send