Robert Strauch
Robert Strauch

Reputation: 12896

JMeter JSR223 preprocessor cannot parse JSON with placeholder

In a JMeter test plan I need to do the following...

Generate TID puts a more or less random number into vars.tid.

The Calculate checksum preprocessor calculates a checksum which is put into vars.checksum. This checksum, however, depends on the tid and further data (var1 and var2) from the JSON payload in Send request.

Example for the JSON body

{
  "event": {
    "checksum": "${checksum}",
    "tid": ${tid},
  },
  "data": {
    "var1": "value1",
    "var2": "value2
  }
}

Calculate checksum

import groovy.json.JsonSlurper
 
def jsonSlurper = new JsonSlurper()

def requestBodyString = sampler.getArguments().getArgument(0).getValue();
def json = jsonSlurper.parseText(requestBodyString)

def tid = vars.get('tid')
def checkusm = calculateChecksum(tid, json.var1, json.var2)

vars.put('checksum', checksum)

Now, the JsonSluper seems to no actually parse the content because of "tid": ${tid} which is not valid JSON. As a consequence, the checksum won't get replaced before sending the request.

Do you have any ideas how to work around this?

Generate TID

Upvotes: 0

Views: 460

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

If you need to resolve the "placeholders" and replace them with the relevant variables values you need to use __eval() function in conjunction with __FileToString() function and pass the result to the JsonSlurper:

enter image description here

More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

Upvotes: 0

Related Questions