How to pass values in the request body from the array of input values

Hi @hannah.neil @praveendvd @bpricilla

i have a small query and i am looking for your suggestions on this .

Problem :slightly_frowning_face:

i am working on the post method and as a part of it i have to send the body.Body looks like this

{
  "id": int,
  "maxWeight": int,
  "minWeight": int,
  "disabled": boolean
}

i am trying to acheive something like this

i kept the below code in the pre-request at folder level

var weightCategory = ["1-0-50","2-51-100","3-101-150","4-150-200"];

pm.environment.set('weightCategory', weightCategory);

pm.environment.set("weight", weightCategory[Math.floor(Math.random() * weightCategory.length)]);

Now for every run i am getting the one out of the listed weightcategory, fine til this point.:smiling_face_with_three_hearts:

What i want to acheive is

Suppose the weightcategory i got is β€œ1-0-50” , then i want to see these values in request as below

{
  "id": 1,
  "maxWeight": 50,
  "minWeight": 1,
  "disabled": randomboolean
}

i kept the below code in pre-request tab at request level

pm.environment.set("data",pm.environment.get("weight").split("-"))
console.log(pm.environment.get(data[0]));

But in console i am getting undefined when i am expected it to log β€œ1”

image

Can Someone help me on how i can acheive this

HappyTesting.:smiling_face_with_three_hearts:

Hi there,

You’re really close! When getting the data variable back, you need to reference the name of the environment variable (i.e. "data" needs to be in quotes). Also the bracket was in slightly the wrong place - you need to select the 0th element after you have retrieved the variable, like this:

console.log(pm.environment.get("data")[0]);

This gives me a 1 in the console :partying_face:

4 Likes

Thanks a lot for a quick solution @neilstudd , it is working .:smiling_face_with_three_hearts:

2 Likes