The toolkit of the REST API developer

While you are developing your own REST API, or writing an integration for a third-party one, you might want to test it before you start writing your code. There are a handful of tools that will help you with this task, whether you want to use your browser, or you are a fan of the command line.

Testing APIs with browsers

There are actually several add-ons that allow you to perform HTTP requests from browsers, depending on which one you use. Some famous names are Advanced Rest Client for Chrome and RESTClient for Firefox. At the end of the day, all those clients allow you to perform the same HTTP requests, where you can specify the URL, the method, the headers, the body, and so on. These clients will also show you all the details you can imagine from the response, including the status code, the time spent, and the body. The following screenshot displays an example of a request using Chrome's Advanced Rest Client:

Testing APIs with browsers

If you want to test GET requests with your own API, and all that you need is the URL, that is, you do not need to send any headers, you can just use your browser as if you were trying to access any other website. If you do so, and if you are working with JSON responses, you can install another add-on to your browser that will help you in viewing your JSON in a more "beautiful" way. Look for JSONView on any browser for a really handy one.

Testing APIs using the command line

Some people feel more comfortable using the command line; so luckily, for them there are tools that allow them to perform any HTTP request from their consoles. We will give a brief introduction to one of the most famous ones: cURL. This tool has quite a lot of features, but we will focus only on the ones that you will be using more often: the HTTP method, post parameters, and headers:

  • -X <method>: This specifies the HTTP method to use
  • --data: This adds the parameters specified, which can be added as key-value pairs, JSON, plain text, and so on
  • --header: This adds a header to the request

The following is an example of the way to send a POST request with cURL:

curl -X POST --data "text=This is sparta!" 
> --header "Authorization: Bearer 8s8d7bf8asdbf8sbdf8bsa" 
>  https://api.twitter.com/1.1/statuses/update.json
{"errors":[{"code":89,"message":"Invalid or expired token."}]}

If you are using a Unix system, you will probably be able to format the resulting JSON by appending | python -m json.tool so that it gets easier to read:

$ curl -X POST --data "text=This is sparta!" 
> --header "Authorization: Bearer 8s8d7bf8asdbf8sbdf8bsa" 
>  https://api.twitter.com/1.1/statuses/update.json 
> | python -m json.tool
{
    "errors": [
        {
            "code": 89,
            "message": "Invalid or expired token."
        }
    ]
}

cURL is quite a powerful tool that lets you do quite a few tricks. If you are interested, go ahead and check the documentation or some tutorial on how to use all its features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.178.151