1
votes

With a script groovy, written in a BeanShell PreProcessor, I generate a random JSON object with some parameters. I am not able to include this object created (I've used JsonBuilder in the script) in the body of HTTP POST request in JMeter. This is a snippet of my script where I create my JSON object:

...
def json = new JsonBuilder();
def root = json parameter1: value1, parameter2: value2, parameter3: value3, parameter4: value4;

bsh.shared.root.process();
vars.put("BODY", root.toString());

I want to pass this object in the BODY DATA section of HTTP POST request in JMeter:

${BODY}

This is the response data that is generated:

The request content was malformed:
Unexpected character '$' at input index 0 (line 1, position 1), expected JSON Value:
${BODY}
^
1

1 Answers

0
votes

You cannot use Groovy in the Beanshell Pre-Processor as Groovy and Beanshell are different beasts.

For example the def keyword you're trying to use exists in Groovy but doesn't exist in Beanshell and if you open jmeter.log file you will see the following:

BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``def json = new JsonBuilder();'' : Typed variable declaration : Class: def not found in namespace

As your script fails in the very first line your ${BODY} variable is not defined and is being sent as it is by the HTTP Request sampler hence you're getting this error.

Switch to the JSR223 PreProcessor, ensure to choose groovy from the "Language" dropdown and double check that there are no errors in the jmeter.log file.

More information: