I wear a lot of hats these days, and unfortunately I don’t have my web developer hat on every day. Thus, I am posting this as much for myself as for anyone else, so that I will always have an easy way to find it the next time I need it, which will surely happen again.
These days it is very common to be working on an API that you just don’t have an interface for yet. There are plenty of ways to test APIs, but a tool that I like to use when unit testing is Fiddler. This tool has been around for quite a while (10ish years?), was created by Eric Lawrence, and is currently owned by Telerik (whom I have no affiliation with). It’s really great for watching all your web traffic, among other things.
Today I’m going to demonstrate how to test a RESTful API on HTTP using Fiddler.
For the record, I’m making some fairly common assumptions. I’m assuming everything is in JSON. It doesn’t have to be, but in almost every case these days it is, so it is a safe assumption. I’m also assuming that if you’re reading this you have a basic understanding of how an HTTP server works and what JSON is, so I don’t plan on explaining any of that. Remember this is a selfish post for my own reference later. :)
So, first thing you need is Fiddler. Go download it, and install it. It’s free, and I linked it in the first paragraph. In case you missed that though, it’s here.
Now you need an API to test. Normally you will be writing one, but I’m going to use a free one here so that everyone can try it. This is from the folks at World Weather Online, where I registered to use their free API that will get local weather data for the postal code I send in.
First the easy way, doing a get.
Here is the resource:
The Postal code I want is: 28078 (Huntersville, NC)
My APP Key for the API (5 calls per second, 500 call per hour allowed): baey8kmn59dqtesenfvy24y6
And so the final URL that I use to get my data is:
Notice I’m also telling them I want JSON format, and I want one days worth of data.
In fiddler, go to the “Composer” tab. Make sure “GET” is selected in the dropdown on the left, and paste the URL in the wide textbox in the center.
Then press execute! What you want is the 200 response code (aka thumbs up) on the left panel:
Now double click on that row, and you’ll get a world of information to review in Fiddler:
But, what if the API accepts POST or PUT and I have to send parameters. Well this is what I always have to look up myself, so you would follow a slightly different procedure.
Now, this API doesn’t support this, so you can’t really try it. generally POST is making a change to something, or adding something, so keep that in mind.
You would take the base URL, something like: http://api.worldweatheronline.com/free/v1/weather.ashx
From here, it’s the same. Click execute and hope for the 200 response code. If you get something else (I think for this one you will get a 403, which is access denied). Even that tells you what you want to know though, whether your post worked, and why not if it didn’t.
So here it is, any time I want I can come back and look at what the headers are supposed to be (that’s the part I always forget…)
Until next time,