Search code examples
javascriptjsonjmeter

How to set JSON request body in JMETER variable


In my JMETER - Perfroemcne script,I have a JSON request body same request body first send for x-signature genration then send orignal request with x-signature both have differnt API end point one for x-signature genration oter for orignal call.

JSON Request:

Let requestBody = {
  "id": ${U_id},
  "username": "${U_username}",
  "firstName": "${U_firstName}",
  "lastName": "${U_lastName}",
  "email": "${U_email}",
  "password": "${U_password}",
  "phone": "${U_phone}",
  "userStatus": 1
}

vars.put("my_request",requestBody);

Following error found in Jmeter Logs:

2023-04-16 08:19:31,394 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
javax.script.ScriptException: <eval>:1:4 Expected ; but found requestBody
let requestBody ={
    ^ in <eval> at line number 1 at column number 4
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477) ~[jdk.scripting.nashorn:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.asCompiledScript(NashornScriptEngine.java:503) ~[jdk.scripting.nashorn:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.compile(NashornScriptEngine.java:189) ~[jdk.scripting.nashorn:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:211) ~[ApacheJMeter_core.jar:5.5]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:45) ~[ApacheJMeter_components.jar:5.5]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:978) ~[ApacheJMeter_core.jar:?]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:561) ~[ApacheJMeter_core.jar:?]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) ~[ApacheJMeter_core.jar:?]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) ~[ApacheJMeter_core.jar:?]
    at java.lang.Thread.run(Thread.java:834) ~[?:?]

I tried above code but we got error.Can any bosy help me in this.

Save request body in one Jmeter variable and first use for x-signature then for orignal call. one more this we set JSON body date form other Jmeter variables.


Solution

    1. I don't think you can use let with the JSR223 Test Elements, you will need to use var statement instead

    2. The value of the variable needs to be a string so you need to surround it with quotation marks and escape special characters. Something like:

      var requestBody = '{\n' +
              '    "id": ${U_id},\n' +
              '    "username": "${U_username}",\n' +
              '    "firstName": "${U_firstName}",\n' +
              '    "lastName": "${U_lastName}",\n' +
              '    "email": "${U_email}",\n' +
              '    "password": "${U_password}",\n' +
              '    "phone": "${U_phone}",\n' +
              '    "userStatus": 1\n' +
              '}'
      
      vars.put("my_request", requestBody);
      
    3. Since JMeter 3.1 it's recommended to use Groovy for scripting from performance perspective. Moreover JavaScript support was removed in Java 15 so you won't be able to use JavaScript when/if you upgrade your Java. So consider migrating to Groovy, the performance will be much better: Apache Groovy: What Is Groovy Used For?