Postman: Iterating through requests with various input data sets to dynamically generate the request body

I am facing a challenge with my login API request that requires 3 parameters (userName, password, and remember) in the request body. Out of these parameters, userName and password are mandatory, while remember is optional. The input data is being pulled from a CSV file during runtime, with the following content:

https://i.sstatic.net/6rRYS.png

My goal is to use this request with different inputs, generating the request body accordingly. Here is an example of how the request body should look for each iteration:

Iteration 1,

{
    "userName": "{{userName}}",
    "password": "{{password}}"
}

iteration 2,

{
    "userName": "{{userName}}",
    "password": "{{password}}",
    "remember": {{remember}}
}

iteration 3,

{
    "userName": "{{userName}}",
    "password": "{{password}}",
    "remember": {{remember}}
}

In addition, specific assertions need to be implemented for each iteration based on the response received. I would appreciate any guidance on how to achieve this.

Answer №1

To access the iteration data for each line in the CSV file, you can utilize the special "data" variable provided.

As a result, a JSON object is generated that closely matches the format required for your request.

{userName: "test1", password: "test@123", remember: ""}

The issue lies in how it interprets the CSV header, causing all headers to become keys in the object regardless of whether they contain relevant data or not.

Thus, it's essential to clean up this output before using it as the body of the request.

let iterationData = data;
// console.log(iterationData);

for (var key in iterationData) {
    if (iterationData[key] === "") {
        delete iterationData[key];
    }
}

// console.log(iterationData);

pm.request.body.raw = JSON.stringify(iterationData);

If you configure your body as raw/JSON and keep the content empty, including this script in the pre-request section will automatically generate the necessary body for you.

https://i.sstatic.net/2EgEy.png

Answer №2

If you want to store your CSV content and loop over it in Postman, consider using Pre-request scripts and Variables (you can set variables in pre-request scripts). Using setNextRequest will help you loop over your requests. Make sure to check out the following resources that may answer your question:

Pre-request scripts and Variables

Learn more about looping over a request with an example here

For additional insights, visit these links:

Running a request multiple times with different data sets

How to loop through an array and use its values in a request

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting issues with adding child elements in JavaScript

Styling with CSS: ul #allposts li{ height: 50px; width: 50px; background-color: yellow; border: 1px solid yellow; } Creating HTML structure: <ul id = "allposts"></ul> Adding JavaScript functionality: var container = documen ...

Deleting an element from HTML using jQuery

In the midst of creating a system that allows users to construct their own navigation structure, I have encountered a stumbling block. The idea is that when a user lands on the site, they are presented with a list of available topics from which they can ch ...

Tips for importing a json response into PHP

After successfully bringing all the results from fetch.php to my bootstrap modal HTML screen using JSON, I encountered a problem. I want to run a MYSQL query with a value from the same JSON used for the modal, but I'm unable to assign this value to a ...

Is there a way in JavaScript to format an array's output so that numbers are displayed with only two decimal places?

function calculateTipAmount(bill) { var tipPercent; if (bill < 50 ) { tipPercent = .20; } else if (bill >= 50 && bill < 200){ tipPercent = .15; } else { tipPercent = .10; } return tipPercent * bill; } var bills = ...

What is the process of retrieving a dictionary from a JSON file using Python?

I have written the following code to implement my requirements: import json json_data = [] with open("trendingtopics.json") as json_file: json_data = json.load(json_file) for category in json_data: print(category) for trendingtopic in catego ...

Tips for transferring HTML code to a controller

Currently facing an issue while working with MVC and attempting to store HTML code from a view in a database field. In the JS section of my MVC solution, I have the following code snippet: var data = { id_perizia: $("#id_perizia").val(), pinSessione: $("# ...

The radio input is not being properly checked using ng-checked in Angular

The ng-checked attribute is causing issues in the application, specifically with radio buttons. It seems to be working fine for checkboxes but not for radio buttons. <input type="radio" class="radio" name="job_class_radio" ng-checked="key===jobClassDat ...

When running the PHP script, the output is shown in the console rather than in the

Here is a PHP script snippet that I am working with: <?php add_action('wp_ajax_nopriv_getuser', 'getuser'); add_action('wp_ajax_getuser', 'getuser'); function getuser($str) { global $wpdb; if(!wp_verif ...

Changing the Displayed Content with a Simple Click of a Link

Layout Overview In the process of developing a tool to streamline work tasks, I want to ensure that I am following best practices. My goal is for the website to initially display only column A, with subsequent columns generated upon clicking links in the ...

The lookAt method in THREE.js is not functioning properly when called after the rendering process

The code snippet below seems to be causing some issues. It requires jquery and three.js to function properly. The problematic lines are as follows: // change the view so looking at the top of the airplane views[1].camera.position.set( 0,5,0 ); views[1].ca ...

What is the process for transferring a Pulumi Output<T> to the container definition of a task in ECS?

When creating a generic ECS service that deals with dynamic data, it is important to note that the containerDefinition within a Task Definition must be provided as a single valid JSON document. The code snippet for this setup looks like: genericClientServi ...

The functionality of Jquery-chosen appears to be malfunctioning when applied to a select element

I am encountering an unusual issue with Jquery-Chosen. There is a multi-select box within a pop-up where the options are populated using an ajax call. Strangely, Jquery-Chosen does not seem to work on it. However, if I use a static multi-select box in the ...

Guide on using jQuery to incrementally cycle through an array using a button

I am facing an issue with iterating through an array of objects using a button. The iteration is skipping 2 on each click instead of moving to the very next record. Can someone help me troubleshoot this problem? Or suggest a better approach to iterate thro ...

Using opening and closing curly braces within a PHP foreach loop

I am facing an issue with formatting an array in PHP. The array structure is as follows: Array ( [0] => Array ( [team1_score] => 10 [team2_score] => 5 [round_number] => 1 [teamtitle1] ...

Is there a way to configure MaterialUI XGrid filters to target and filter by the renderCell parameters instead of the backend data source?

While utilizing MaterialUI XGrid to showcase rows of information, I am facing an issue with filtering. Currently, filtering can only be done based on the backend row values rather than what is displayed in the cell. For instance, consider a column named U ...

Delete one object and then sequentially rename all remaining objects

object This is the object I retrieved. How can I remove module_1 object and rename the module object? For example, remove module_1 and rename module_2, module_3... to module_1, module_2... `{ "module_1": { "modulename": "mat ...

I am having trouble generating a pie chart with CodeIgniter

I am struggling to display a pie chart of expenses in percentage with the code provided below. When I run the code, it shows a blank page instead of the desired chart. I would greatly appreciate any help or corrections on the code as I am feeling very conf ...

How can I generate a sorted array of objects in a JSON output using PowerShell?

Is it possible to generate a JSON output with an array of objects sorted in ascending order based on the "count" property? I need to maintain the original $result object structure, without caring about the order of "Good" or "Bad", My goal is to sort the o ...

Transferring information to the controller and refreshing the interface (Single-page design)

I'm stuck on a personal project and could really use some help. If anyone has any feedback, it would be greatly appreciated. Thanks in advance. In my index.php file, I have a div with the id main-container. Inside this container, there are two separa ...

Unable to display tags with /xoxco/jQuery-Tags-Input

I'm experimenting with the tagsinput plugin in a textarea located within a div that is loaded using the jquery dialog plugin. The specific plugin I am using is /xoxco/jQuery-Tags-Input. After checking that the textarea element is ready, I noticed th ...