Parameterize Values in Postman
Recently created a postman collection for some of the business flows in a mobile application from API level, after intercepting the calls in-between the secured endpoint and the mobile app as described in a previous post.
While creating the request collection manually for various business flows by duplicating some steps, data passing became a time consuming extra work during an execution where each request was to send separately after copying a token or an Id from the previous response to the next request.
Following the manual process of ‘CTRL + C’ and ‘CTRL + V’ for several times made my self dig into and explore an alternative, lazy solution or an approach due to the time and effort that had to invest on after each request is sent to the endpoint.
Even though its not a great discovery for a certain extent; after figuring out the solution to overcome the copy and paste procedure during the execution the idea of documenting the solution to assist another came up to my mind.
Therefore this guide will be useful for anyone who is stepping into Postman or into Scripting with Postman, and to get a thing done easily. For any script-writer with the exposure in JavaScript, this article will be a piece of cake without any doubt apart from the Postman configuration and usage.
For the purpose to explaining the solution, the extraction of an access-token from an authenticated response will be considered through-out this article as “the example”.
- Verification
Initially, before setting up the surrounding make sure to send a request to the endpoint with related headers and body to verify the endpoint, identify the nature of the response and identify the elements needed to be capture.
2. Setup
After ensuring the response of the endpoint and identifying the elements that needs to be capture for the parameters during the execution, setup a new environment under Manage Environment in Postman.
There after; define the keys(parameters) with empty values under the newly created environment as the elements needed to capture are already decided.
3. Script
Capturing is where the knowledge of scripting is needed to write down a code snippet for the extraction of each element from the response body. The ‘tests’ tab is given by the Postman to write down customized scripts.
4. Hit
After completing the scripting, click upon the ‘Send’ to hit the request on the endpoint.
5. Capture & Parameterize
Verify the functionality of the written script by opening the ‘Environment Quick Look’ pop-up. If the values shown in the response body are identical to the values of the keys created during the Setup, the Element Extraction and Parameterizing is successful.
Yes! Element extraction and Parameterizing is Successful. But, What is the purpose of parameterizing the values if we are not aware of the way to reuse the captured values from the parameters/keys/variables to another request header, body or endpoint URL?
6. Reuse
Identify the next request in sequence which needs to carry the captured element value. Build the particular request and verify the availability of the new endpoint by copy and pasting the key-value for the verification purpose of the endpoint.
If the endpoint returns a success or a 200 OK as the HTTP response, proceed to parameterize the values in the request header, body or endpoint with the keys defined in the environment by using the key in-between double curly braces {{}}.
Ex: {{access_token}}, {{refresh_token}}
Feel free to leave your feedback and comments, which can lead us for a friendly discussion below.