skip to Main Content

I have a login API request with 3 parameters(say userName, password & remember) in the request body. In that 2 parameters(userName and password are mandatory, remember parameter is optional). The input is read from the CSV file during the runtime and data in the file is as below.

![enter image description here

Now I want to use the same request for the given inputs and based on the input request body should be formed.
Request : POST https://gateway.com/v1/login
For example,request body should be like below for different iteration.

Iteration 1,

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

iteration 2,

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

iteration 3,

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

and assertions should be specific to each iteration based on the response returned. Can someone help how this can be achieved.

2

Answers


  1. You could use Prerequest scripts and Variables in postman to store your csv content and then loop over it. (You can set variables in prerequest scripts)

    Using setNextRequest should allow you to loop over your request (example here)

    Finally, be sure to read this content that seems to answer your question 🙂

    https://community.postman.com/t/running-a-request-multiple-times-with-different-data-sets/1064
    or
    https://community.postman.com/t/how-to-loop-through-array-and-use-its-values-in-a-request/6771

    Login or Signup to reply.
  2. The iteration data for each line in the CSV is available in the special "data" variable.

    This creates an JSON object (which is nearly in the correct format for your request).

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

    The problem is that it reads the header of the CSV, so you will always have the headers as keys in the object, whether they contain data or not.

    Therefore, you will need to clean this up before it can be used as the body for 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 set your body as raw/JSON but leave the content blank, then if you have this code in the pre-request script, it will create the body for you.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search