Test Akamai API with postman and Javascript #3

It's time to share full Javascript code for "Pre-request Script" feature of postman. To implement code for Akamai API, we do have to use "SHA256" hash algorithm and "HMAC" message authentication code. Javascript itself does not provide such a functionality. So we need to get some help from open source libraries. Very famous one for this purpose in Javascript world is CryptoJS library. Fortunately, postman support embedded CryptoJS and we can use it with prefix "CryptoJS", e.g. "CryptoJS.SHA256(Data)".

// Add created authorization header as a Header of postman collecitons postman.setEnvironmentVariable('authorizationHeader', AuthorizationHeader);}

submitRequest();

If you have interests, you can deep dive into each of code lines. But if you wants to use it with postman, just follow steps described below. I assumed you've already prepared postman environment for your contract.

[ Step1. Create a new collection ]

Before we define specific API request, we need to create collection. You can find folder icon on the left pan. Click it to add a new collection. This is not technical at all :-).

[ Step2. Add a new request ]

A collection is a container for requests. You can think of a request as a specific call for API method. I've picked up a purge method from developer.akamai.com. This method required to be called "POST". Please fill out URL box with your base URL and path, /ccu/v2/queues/default.

Let's back to the top of this article to copy Javascript code to be filled with "Pre-request Script". All you need to do is Ctrl-C then Ctrl-V. Once it's filled with script, blue bullet will be there on the right side of "Pre-request Script" tab.

We need to add some headers into headers tab. As I mentioned earlier, double curly brace will be changed values from environment or code. Code line #132 calls postman.setEnvironmentVariable method to set "authorizationHeader" value. Don't forget to add "Content-Type" as a "application/json". Some of Akamai API wants to set Content-Type as another value but generally it is "application/json"

We are almost done to set-up a request. This is purge request with "POST" method. So we need to specify what contents have to be purged from Akamai Intelligent Platform on HTTP request body. Let's move on to "Body" tab and add JSON formatted purge request details.

[ Step3. Do test Akamai API...!! ]

Are you ready to call the request? Let's click blue colored "Send" button on right side of the URL box. It will take a few second to be queued request. Then the result will be displayed on the bottom of the screen.

Cool. Purge request accepted by machines that is in charge of it. If the request worked well, save the request to be used in the future. "Save" button is located beside of "Send" button.

I'm using postman to call several Akamai APIs nowadays. "Purge" is the best one that I love, but I'm using the code for Media Report API and Property Manager API as well. I don't think there's big errors on script, but if you find anything, let me know it. I will make github repository for this script soon and will share it here.