Test Akamai API with postman and Javascript #2

Blog Post created by B-C-ME2SR3 Employee on May 3, 2017

Before we go into "Pre-request Script", let's have easy time to be familiar with postman. I'm not gonna touch every single features of postman but will see features that I've used to build-up Javascript code for "Pre-request Script". 


[ Postman Feature - Environment ]

Postman has a concept for split out several stages like development, QA, production, etc and we call it "Environment". You can find some controls for environment on the upper-right side of postman screen. If you are not using that functionality, this is a good chance to know it.



I'm using this feature to feed API credential information to "Pre-request Script". As you might know, we need to use 4 values to call Akamai API : 1) Base URL, 2) Access Token, 3) Client Token, 4) Client Secret. I'm not an genius who can remember these always. That's why I ask help from environment functionality to remember them on behalf of me. LoL. 


To create environment, click the gear icon right beside eye icon. There you can make a new environment and key-value hash table for your convenience. Javascript code I will share uses "Key" strings to get consequence "Values". Do not make any typo on key if you don't want to get weird error messages.



[ Postman Feature - Variables ]

Postman allows us to access key-value table of each environment. We can get value from specific keys with embedded Javascript object named "postman". Yes, it's very easy to remember. It has some methods but I will focus on "getEnvironmentVariable()" and "setEnvironmentVariable()".


We, Akamai, described specific logics of API call authentication and authorization on the developer portal. I don't want all you to understand each of the steps. All we need to do is "Just do it", right? But if you are have much interests on it, please visit here(API Client Authentication) for more information. 


Key-value pari of environment can be easily accessed by Javascript code as below. I used that method to get 4 required values of Akamai API credential. After fetching it from environment, we can use it inside of Javascript code easily.


// Pick up environment variables into credentials object
var credentials = {
  baseURL: postman.getEnvironmentVariable("baseURL"),
  accessToken: postman.getEnvironmentVariable("accessToken"),
  clientToken: postman.getEnvironmentVariable("clientToken"),
  clientSecret: postman.getEnvironmentVariable("clientSecret")


Another method named "setEnvironmentVariable()" used to extract calculated authorization header that needs to be added HTTP request for API call. You can find below code from my "Pre-request Script".


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


How this extracted value can be used in HTTP request header? Postman provide way to use it in several places. As we need to use it for HTTP request header, I've added it into "Headers" tab of specific API call. With two curly brace, we can use variables. ( e.g. {{authorizationHeader}} )



Okay. Now we are ready to see Javascript code for postman. If you want to use that code more useful way, you need to be familiar with postman. I will share full Javascript code for "Pre-request Script" on next article.


Stay tuned...!


// Previous Article

Test Akamai API with postman and Javascript #1 

// Next Article

Test Akamai API with postman and Javascript #3 


- NoPD -