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.
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.
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.