Chapter 3
3.1. API Request tools
cURL (Client URL library)
cURL is a command line tool and library for data transfer. It supports a wide range of protocols including HTTP, FTP, SMTP and IMAP.
cURL from the command line is an easy way to test and run API calls.
The first thing you want to be sure of is that you have cURL installed on your system. If you run on OS X (Mac) or a Linux OS, you most probably have cURL preinstalled. A quick way to check is to open your console and run:
curl -V
from the command line. That is uppercase V by the way. The V is an option character that represents version. They are also called flags and are case sensitive. The command should output your cURL version number. If you get an ‘unrecognized command’ error, you don’t have cURL installed.
To install on Mac or Linux,
- Download the source from http://curl.haxx.se/download.html
- Open your console and change to the directory the file was downloaded to
-
Extract the compressed file
tar zxf curl-7.46.0.tar.gz
(Rename curl-7.46.0.tar.gz to the compressed source file you get to download)
-
Change directory to the extracted directory
cd curl-7.46.0
-
Run the make file and install
make && make install
For Windows, download the binary directly from http://curl.haxx.se/download.html (down the page). Once you extract the file, you will need to add the directory containing the curl.exe file to your PATH environmental variable so that you won’t have to always change directory to the curl.exe directory every time you need to run a cURL command. I recommend you move this directory to a location more permanent - so not your Desktop or Downloads folder.
To set the PATH:
- Open your Control Panel, go to System, Advanced system settings and click Environmental Variables. Scroll down the System variable list till you get to Path.
- Double click on it. You should get the Edit System Variable popup.
- Place your cursor at the end of the Variable value content (if not there already). We will be adding to the existing content.
-
Enter a semi-colon followed by the full path to the directory containing your curl.exe. If for example, I moved it to my Documents folder and the curl.exe directory is
curl_7_46_0_openssl_nghttp2_x64
, I simply add this to the content:;C\Users\kehers\Documents\curl_7_46_0_openssl_ng http2_x64
Don’t forget to add the semi-colon.
1. Making a GET request with cURL
To make a GET request, open your console and type curl {url}
(I used the backslash after OX49 to escape the space before 5NU. Another way I could have done this is to quote the endpoint like this: curl 'https://api.postcodes.io/postcodes/OX49 5NU'
)
cURL will return the response body. You should see something like this:
If you want to see the full response including the status and header lines, add an i flag.
2. Making a POST request with cURL
Let’s do a bulk post code lookup. First, we have to tell cURL we will be doing a POST request by using the X flag. To add the POST data, we use the d flag. To make a bulk post code lookup, postcodes.io requires a JSON object containing an array of the post codes. If we therefore have the post codes PR3 0SG
and OX49 5NU
, we have to construct a JSON object like this as our POST data:
One more thing, we need to add a content-type header to say the POST data we are sending is JSON. For that, we use the H flag.
It’s straightforward, isn’t it?
There is so much more you can do with cURL. You will see more examples as we move on. To recap, here are the cURL flags we looked at:
- V - Shows version information.
- X - Specifies a custom HTTP method to use.
- d - Specifies data to be sent for the request. By default, the data is sent to the server using the content-type application/x-www-form-urlencoded.
- i - Includes the HTTP-header in the response output.
- H - HTTP headers to include in the request.
Note that the flags are case-sensitive.
-
Online Tools
There are online tools we can also use for API calls. One I really recommend is Postman. Postman is a Google Chrome extension for making API requests. Visit the website (getpostman.com) and follow the Get it link to install it from the Chrome store.
Postman is easy to use. You add the request URL, HTTP method and send your request.
 And there is more you can do. You can set headers, add message body and set authorization method and parameters.
If you don’t use Google Chrome, hurl.it is a good online tool you can use as an alternative. It supports different HTTP methods, authentication and you can enter custom headers.