Skip to content

How To Use Query Params In Postman

API Testing Blog

Query Parameters: A Powerful Tool for API Testing in Postman

Query parameters are crucial for defining dynamic requests in API testing. They allow you to modify the data retrieved from an API endpoint by adding extra information to the request URL. This guide will walk you through the process of effectively using query parameters in Postman.

Understanding Query Parameters

Query Parameters are key-value pairs appended to the URL after a question mark (”?”). They are separated by ampersands (”&”). For example, consider this URL: https://api.example.com/users?page=2&limit=10

  • page=2: This parameter specifies that we want to retrieve data from the second page.
  • limit=10: This parameter indicates we want to retrieve only the first 10 results.

Adding Query Parameters in Postman

1. Define Your Endpoint:

  • Navigate to the “Request” tab in Postman.
  • Enter the base URL of your API endpoint in the “URL” field.

2. Adding Query Parameters:

  • In the “Params” tab, click on the “Add Param” button.
  • Enter the parameter key in the “Key” field.
  • Enter the parameter value in the “Value” field.
  • Repeat steps 2 and 3 for each additional query parameter.

Example:

  • Endpoint: https://api.example.com/products
  • Params:
    • category=electronics
    • price=50

Request URL: https://api.example.com/products?category=electronics&price=50

Using Variables for Query Parameters

Postman variables offer a more dynamic way to manage query parameters.

1. Create a Variable: * In the “Variables” tab, click on “Add Variable”. * Enter a unique variable name (e.g., page). * Enter the initial value for the variable (e.g., 1).

2. Use Variables in Your Request: * In the “Params” tab, replace the hardcoded values with your variables: * Key: page * Value: {{page}}

Example: * Variable: page * URL: https://api.example.com/users?page={{page}} * To change the page number, simply update the value of the page variable in the “Variables” tab.

Utilizing Pre-request Scripts

Pre-request scripts allow you to modify the request dynamically before sending it. This is particularly helpful for dynamic query parameters.

1. Accessing and Modifying Variables:

  • In the “Pre-request Script” tab, use pm.variables.set() to assign a value to a variable.

Example: * Pre-request Script: javascript pm.variables.set("currentPage", 2);

2. Using Variables in Query Parameters: * In the “Params” tab, use your pre-request script variable: * Key: page * Value: {{currentPage}}

Example: * Pre-request Script: ```javascript pm.variables.set(“currentPage”, 2);

```
* **URL:** `https://api.example.com/users?page={{currentPage}}`
* This will send a request to `https://api.example.com/users?page=2`.

Working with Collections and Environments

Postman lets you group requests and variables into collections. They can be shared and reused.

1. Create a Collection: * Click on the “Collections” tab and select “Create Collection”. * Provide a name for your collection and add requests to it.

2. Create an Environment: * Click on “Environments” and select “Add Environment”. * Define an environment name and add variables to it.

3. Utilize Collections and Environments in Your Tests: * Select your collection and environment. * The variables defined in the environment will automatically be accessible to all requests within the collection.

Testing Query Parameter with Assertions

1. Adding Assertions:

  • Navigate to the “Tests” tab.
  • Write test scripts using pm.test() to verify the response.
  • Use pm.response.json() to access the data in the response.
  • Use pm.expect() to perform assertions on data retrieved through query parameters.

Example:

  • Request: https://api.example.com/products?category=electronics
  • Test Script:
    pm.test("Verify category", function() {
    var jsonData = pm.response.json();
    // Check that all the products in the response are in the 'electronics' category
    pm.expect(jsonData.every(product => product.category === 'electronics')).to.be.true;
    });

Conclusion

Mastering query parameters in Postman is essential for comprehensive API testing. They provide the flexibility to craft dynamic requests, test API behavior under various conditions, and validate data returned in the response. This guide has covered the fundamental ways to use query parameters in Postman, from simple addition to complex dynamic requests using variables, pre-request scripts, and collections. Utilize these techniques to create efficient, robust, and reliable API tests.

API Testing Blog