Saturday, October 12, 2019

Postman API – Tips and Tricks

I have seen many people using POSTMAN tools but very few of them know how to exactly use all the features of POSTMAN application. So, in this article, I am going to share a few tips/tricks which can be really helpful in our API testing.

  • Set Environment Variables


Let’s assume we have an endpoint to get some data after authenticating yourself bypassing username/password.

Now, you have to test this endpoint which is deployed on multiple environments like a local machine, test environment and on SIT environment as well. Also, the credential is different for each environment.

So, how are we going to test it?

Most of the time, I have seen that people create one-one requests for each environment or create one request and then modify the existing username/password and hostname to point to a different environment.

The Simple Solution is to set changing parameters as an Environment variable and switch environment to test for different regions.

E.g. http://<HOSTNAME:POST>/token and <username>/<password> as basic Authentication 


How to set environment variables?

  • Click "Manage Environments" icon in the upper right corner of the Postman app.
  • Click the Add button.
  • Click "Environment" and enter a name for the new environment.
  • Add the variables you want to save as key-value pairs.
  • Click the Add button.





Selecting an active environment

  • Click the dropdown menu in the upper right corner of the Postman app to select an active environment, or type in the environment name.
  • Once you select an environment, you can access variables in the active environment scope.

All the parameters get changed as we switch from one environment to another one.

  • Set Global variable


Global variables provide a set of variables that are available in all scopes. You can have multiple environments, but only one environment can be active at a time with one set of global variables.

To manage global variables, click the gear icon in the upper right corner of the Postman app and select "Manage Environments".

Click the Globals button at the bottom of the modal to bring up a key-value editor to add, edit, and delete global variables.




 

  • Set Response to the global variable


There are so many scenarios where we wanted to add one of the API responses as a request parameter to the second request like getting token from one API and then passing this token to another endpoint to access the resources.

Assuming, I am hitting API to get access token and then setting access-token as a global variable. 




  • Once you get the access_token in your response then go to Tests tab.
  • Click on ‘set a global variable’ or add scripts as shown below
    • let response = pm.response.json();
    • pm.globals.set("access_token", response.access_token);
  • To access this variable in some other request, just pass the access_token parameter as {{access_token}} as we have done something similar for dev_hostname.
E.g. 


  • How to pass the path variable dynamically

I guess this is one of the major mistakes I have seen people doing it i.e. passing hardcoded path variable in the URL request

The below diagram will show you the correct way of passing it. Using this, you can update params value very easily.

http://hostname/users/:userId

To edit the path variable, click on Params to see it already entered as the key. Update the value as needed.



  • Save custom headers

You can save commonly used headers together in a header preset. Under the Headers tab, you can add a header preset to your request when you select "Manage Presets" from the Presets dropdown on the right. 





  • Check all request/response send to API

Goto View-> Show Postman Console



No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...