1
votes

Using JSON Extractor, extracted multiple values from response body. Debug Sampler -> For eg shows these 3 values ID_1=212 ID_2=211 ID_3=225

How to use each of these in the next HTTP Post Request? Like how to use one value for each run?

POST request body {"id"={$ID}} -> doesn't work.

However, {"id"={$ID_1}}, {"id"={$ID_2}}, {"id"={$ID_3}} individually works.I want to avoid manually changing the POST body request everytime.

2

2 Answers

0
votes

Add a BeanShell PostProcessor right after you JSON Extractor (in the same level, as a child of the first request) with the below code in code area

int matchNr = Integer.parseInt(vars.get("ID_matchNr"));// ID is the reference name of your JSON Extractor

String ID = "";
for(int i = 1; i <= matchNr; i++){
    if(i == 1){
     ID = "{\"id\"=" + vars.get("ID_" + i) + "}, ";
    }

    else if(i == matchNr){
     ID = ID + "{\"id\"=" + vars.get("ID_" + i) + "}";
    }

    else{
         ID = ID + "{\"id\"=" + vars.get("ID_" + i) + "}, ";
    }

    vars.put("IDs", ID);
}

Now use the variable ${IDs} in your POST request body, the value will be something like this {"id"=1}, {"id"=2}, {"id"=3}, {"id"=4}

0
votes

You can use for each loop for your requirement. Check jmeter ForEach Controller. The first example jmx of ForEach can met your requirement. Below reqEx to fetch all. Then forEach for all regEx as input in "inputVar" and fetch it using returnVar. Pass returnVar to your next request that is HTTP 2. enter image description here

Hope this helps.