Maximizing Efficiency with Global Variables in Postman API testing

Rehan KhanDyn365CE9 months ago14 Views

Introduction

Postman is a powerful tool for testing APIs, enabling developers to streamline their testing workflows and improve productivity. In my recent implementation, where I have dealt with multiple OAUTH secured API URLs .
OAUTH bearer token API is an endpoint provided by OAUTH 2.0-compliant authorization servers that allows clients to obtain access tokens required to access protected resources on behalf of the resource owner.

One of the key features in postman that contribute to this efficiency is the use of global variables. In this quick tip blog post, we’ll explore how leveraging global variables in Postman can enhance your testing experience and save valuable time.

Why Use Global Variables?

Using global variables offers several benefits:

  • Efficiency: Eliminate the need to manually update values in multiple requests by centralizing them in global variables.
  • Consistency: Ensure consistency in data across requests, reducing the risk of errors or inconsistencies.
  • Flexibility: Quickly switch between different environments or configurations by updating variables at the global level.
  • Reusability: Reuse variables across collections or projects, saving time and effort in defining and maintaining configurations.

Power apps developer working with dataverse WEB API might also feel the pain to generate a bearer token and passing it in the subsequent API call for authorization .

In Today’s blog we will see how we can use bearer token across all the API request in postman through a global variable and few lines of script.

I will be using dataverse WEB API so I have the client id and client secret for the WEB API setup in a postman. To register Dataverse WEBAPI please refer this blog.

  1. Suppose we are having a token generation request in postman where we have to manually copy the token and pass it in the header of another call .

2. Now create a variable where we will save the access_token generated in above request .We will be creating a Global Variable , A value in global variable is accessible across multiple request in multiple collection.

3. Navigate to Environment on the left Menu Environments > Globals . Add new variable.
I have created a new variable reycrmblogtoken , and set the type as secret.

That’s it our our Global variable is created , lets Add value of access_token in this dynamically using script.
4. Navigate to the request for bearer token generation. Open the tests tab under that postman request and write below script for execution .

let responseData=pm.response.json(); // Parse Json 
console.log(responseData); // Log response 
pm.globals.set("reycrmblogtoken", responseData.access_token); // Set access token from response in to the global variable we created

We are done now every time we generate token from this request bearer token will be saved in our Global variable .

Lets try to initiate a Dataverse WEBAPI call , We will try to get accounts from dataverse GET call . First I will send without authentication as seen below we are getting 401 unauthorized error .

Now lets set our token in authentication of this WEB API request . you can use ‘{{
to get your global variable. Below you can see I am setting reycrmblogtoken .

Now lets execute the bearer token request first so that our script executes and set the value in global variable.

Now lets hit the GET API request again .That’s it Now we don’t need to copy-paste the
bearer token and paste . We will just hit the bearer token request and variable will be updated with new bearer token .

This is how we can reuse the token generated in one request across multiple API calls with minimal efforts.

Hope this helps .. Thank you for reading.

Original Post https://msdynamicscrm137374033.wordpress.com/2024/05/12/maximizing-efficiency-with-global-variables-in-postman-api-testing/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...