Postman Tutorial Part 20 – Use Variables in Request Builder OR How To Build Parameterized Request in Postman

Hello Folks,

We have learnt already about :-

What is Variable in Postman?

Global Variables in Postman

Environment Variables in Postman

In this post, we will see how can we use variables in request body to create parameterised request.

What is use of Parameterised Request?

Consider we have an API which registers a patient on any application. You need to pass required patient data like Name, Age, Gender, Address etc. These data will vary patient to patient. Everytime data needs to be changed to register a new patient. So if we construct a request body with hardcoded data in it, we need to keep editing request body for new patient which is not best practise.

Example is below:-

    "name" : "Mr. Clark",
    "Age" : "25",
    "address" : "London"

You will face below problems if you hardcode data in request:-

  1. You need to keep changing data in request body.
  2. You might misplace data.
  3. Another member will not able to edit data properly if not aware about request body.
  4. You can not use data from external sources.
  5. You can not achieve data driven testing.
  6. We must always keep logic and data separately.

All above problems can be resolved by parameterizing request body.

We know that we can create global and environment variables. We can use them in our requests.

Steps to parameterizing request:-

we will parameterized below API:-

Its request body:-

“username ” : “admin”,
“password” : “password123”

Refer here for more details.

  1. Create global/environment variables with required fields in body. It’s not mandatory to put key name same as attributes in request body. It will be advisable to give same name as it will not confuse you. But if you don’t give same name, link it correctly. I created a Environment file named “Auth Data” and added two variables “name” and “password” with correct data.

2. Now parameterised request as below:-

As we have seen already that we can use variables in URL as {{variableName}}. In the same way, we can use in Request body as well.

    "username" : "{{name}}",
    "password" : "{{password}}"

Do not forget to select enviroment name in Environment drop down if not global variables.

Now no need to touch actual request body. Just update value in variable files and it will be reflected automatically.

But you need to write a lot of logic when you have nested json object, json array. We will see all those in upcoming posts.

Collection Link:-

More about API Testing in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Leave a Reply

Your email address will not be published. Required fields are marked *