Raxon
Raxon

Reputation: 33

Modify the request body based on the JSON extractors from previous request in JMeter

Response from the JSON Request A has 2 values columnID and rowID. These values are extracted using JSON extractors. There is another request B and request Body data looks like this:

{
    "columnIDFieldList": [{
    "MyColumnID": "${columnID}",
    "hasValue": true
}],
"rowIDFieldList": [{
    "MyRowID": "${rowID}",
    "hasValue": true
}]
}

I want to modify the request B body data, depending on the JSON extractors from request A.Like if JSON extractor extracts columnID = AD256 and rowID = HG986, the request body of B is

{
    "columnIDFieldList": [{
    "MyColumnID": "AD256",
    "hasValue": true
}],
"rowIDFieldList": [{
    "MyRowID": "HG986",
    "hasValue": true
}]
}

And if JSON extractor extracts columnID = null or no value and rowID = HG986, the request body of B is

{
"rowIDFieldList": [{
    "MyRowID": "HG986",
    "hasValue": true
}]
}

How to achieve this in JMeter.Tried using JSR223 preprocessor for modifying the request body, but is issue is columnID and rowID extracted from 1st iteration is being used in all iterations which is not correct as the values needs to get updated for each iteration. Is there any other ways to achieve this.

Upvotes: 1

Views: 26

Answers (1)

Ivan G
Ivan G

Reputation: 2872

We cannot say what's wrong without seeing your JSR223 PreProcessor code, maybe you're referring JMeter Variables in the Groovy script like ${columnID} and they're compiled and cached, maybe you're incorrectly modifying the body data using sampler shorthand, maybe both.

The easiest way would be doing something like:

def builder = new groovy.json.JsonBuilder()

builder {
    if (vars.get('columnID')) {
        columnIDFieldList([
                [
                        MyColumnID: "AD256",
                        hasValue  : true
                ]
        ])
    }
    if (vars.get('rowID')) {
        rowIDFieldList([
                [
                        MyRowID : "HG986",
                        hasValue: true
                ]
        ])
    }
}

vars.put('payload', builder.toPrettyString())

and they just using ${payload} in the HTTP Request Sampler's "Body Data" tab

More information:

Upvotes: 0

Related Questions