4

How to Use Environment Variables in Postman

 2 years ago
source link: https://blog.bitsrc.io/how-to-use-environment-variable-in-postman-ba7c0f49f22e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Use Environment Variables in Postman

How to use environment variable in postman
Photo by Daria Volkova on Unsplash

Introduction

You might have been using Postman for quite a long time and probably don’t need to read this article and I won’t mind skipping, but a few freshers are quite frequently asking me this question on how they can optimize postman usage do not copy-paste things much time.

Today, I will be talking about how you can optimize your postman request in much good way and not going in much advance charge will be covering basic ideas on the approach.

What is a Postman?

Postman is a tool for API automation using OpenAPI documents. It is an HTTP client and is used for testing APIs. There are more than this, you can connect workflows, manage packages and folders, and many more.

Environment Variables

Since HTTP is a stateless protocol and it does not contain a state so does Postman. Each request is independent of each other and will be using different environments but someday you might want to pass common data that you don’t want to copy-paste so you can make use of Postman here.

Only folder, collection and workspace can use environment variables. You need to save a request to use them.

How to create Env variables

Once you launched your Postman instance, you will be leading to some default screen. By going to details of collection or clicking on edit then you will be directed to the new tab and go to variable section of it then you can edit or create a new environment variable. You can check/uncheck variables when you want to use or not use them.

example of how to create a new environment variable
example of how to create a new environment variable
create env variable

How to use Env variables

To use a variable you need to make {{any_variable}} this syntax and use it.

The following is an example of using environment variables:

Example of using environment variables on url, header and params
Example of using environment variables on url, header and params
use env variables

Now you don’t need to copy-paste values from tab to tab.

How to dynamically create variables

Consider an example, suppose you created an endpoint /login that returns an API key and secret after accepting the credentials and now each request should be passed with the token on the header you need to pass those variables.

So how do we achieve this?

Step 1

Create a new request named /login and add the default setup you want to add. After done, go to the tests tab under the same header, body, and params you will find.

pm.globals.unset("API_KEY");pm.globals.unset("API_SECRET");pm.test("Validate Response", function () {  pm.response.to.have.status(200);  pm.response.to.be.withBody;  pm.response.to.be.json;  pm.response.to.not.have.jsonBody('error');  pm.response.to.have.jsonBody('API_KEY');  pm.response.to.have.jsonBody('API_SECRET');});pm.test("Cache Data", function () {  var json = pm.response.json();  pm.globals.set("API_KEY", "Bearer " + json.access_token);  pm.globals.set("API_SECRET", json.refresh_token);});

Here, we unset two variables (add/remove based on requirement) which we haven’t declared in the variables tab then after writing two test cases. The first is checking if we have desired values in response. The second is setting the global variables with the unsettled key to new values.

Step 2

We have already covered the main point, now let’s use it in any of API as:

Example of how to create dynamic variables and use it in Postman
Example of how to create dynamic variables and use it in Postman
use dynamic variables

You can customize the behavior by going under common authentication and inheriting from a parent in the Authentication tab and using it accordingly.

Conclusion

So now we know how to add environment variables to Postman! I hope you have found this useful. If so, be sure to let me know in the comments.

Have a great day ahead!

Build anything from independent components

Say goodbye to monolithic applications and leave behind you the tears of their development.

The future is components; modular software that is faster, more scalable, and simpler to build together. OSS Tools like Bit offer a great developer experience for building independent components and composing applications. Many teams start by building their Design Systems or Micro Frontends, through shared components. Give it a try →

0*pfbTHSZUhVYJaQxV?q=20
how-to-use-environment-variable-in-postman-ba7c0f49f22e
An independent product component: watch the auto-generated dependency graph

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK