1
votes

I tried a lot in google and did not find any solution. If I missed then I am sorry.

In Jmeter, I am running the same request in a loop. For 'n' number of times. For every request, I need to extract the json response and pass it to next request. I am able to extract the response of last one request and save it to a variable and then pass to next request. I used JSON Path Extractor. I also figured out extracting the response using BeanShell and JSR223 Pre and Post Processors

The thing here is I need to extract all previous responses data and build the request body for the next request, not just last 1 response. Please

I do not want to append the extracted response to a file then pass the data to request from the file.

    Request1 (Requestbody:[]). Response1: Product A 
    Request2 (Requestbody: [Product A]). Response: Product B 
    Request3 (Requestbody: [Product A, Product B]. Response Product C 
    Request4 (Requestbody: [Product A, Product B, Product C]). Response: Product 
.. ... ..... 
Requestn (body: [Product A, Product B, Product C, Product D]....), response: no more products

Any thoughts please

Thanks Jack

2

2 Answers

1
votes

If you need to build a JSON Array from previous responses I would recommend consider using JSR223 PostProcessor (assumes Groovy language) and JSONBuilder class for this.

Groovy has built-in JSON support therefore you will have the full flexibility in reading and writing arbitrary JSON structures.

Example:

Groovy JSON Example

References:

0
votes

hmm.. what a requirement. I wonder what would be the use case. :) I achieved this with a LinkedList and passing the object back and forth between the pre and post processors using getObject and putObject. The below codes contains plenty of debug statements. Please discard them. YOu can also optimize the same.

HTTP Sampler with "BODY DATA" tab having just ${request}

--> Beanshell pre-processor

log.info("Entering preprocessor..");
LinkedList itemsArrayLocal = vars.getObject("itemsArrayLocal");

if ( itemsArrayLocal == null) {
    try {
        itemsArrayLocal = new LinkedList();
        //itemsArrayLocal.add("");      
        vars.putObject("itemsArrayLocal", itemsArrayLocal );
        vars.put("request", "(Requestbody:[" + ""  + "]");
    }
    catch (Exception e) {
        e.printStackTrace();
        log.info(e);        
    }
}
else {  
    String s = "";
        for ( int i=0; i < itemsArrayLocal.size(); i++) {           
            if ( i >= 1) {
                s = s + ",";
            }
            s = s + itemsArrayLocal.get(i).toString() ;
            log.info("i=" + String.valueOf(i) + s);
        }
        log.info("s=" + s); 
        vars.put("request", "(Requestbody:[" + s + "]");
}

--> Beanshell post-processor

log.info("Entering POST PROCESSOR..");
LinkedList itemsArrayLocal = (LinkedList) vars.getObject("itemsArrayLocal");
String o = prev.getResponseDataAsString().substring(2,10);
//log.info("o=" + o);
try {
    log.info("Added..");
    itemsArrayLocal.add(o);
    log.info("Size=" + String.valueOf(itemsArrayLocal.size()));
}
catch (Exception e) {
    e.printStackTrace();
    log.info(e);
}