0
votes

In my JMeter script, I have one HTTP request which has 4 different parameters to be passed in post body. I have corresponding variables. Values of these variables are not available every time, depending on configuration.

If a value is not available, I get an error "bad request". How do I see if a variable is not null and only then pass corresponding parameter in request post body?

2

2 Answers

1
votes

Given you have the following configuration:

Example HTTP Request

and you don't want to send foo parameter if ${bar} variable is not defined

  1. Add Beanshell PreProcessor as a child of your HTTP Request Sampler
  2. Put the following code into the PreProcessor's "Script" area:

    if (vars.get("bar") == null) {
        sampler.getArguments().removeArgument("foo");
    }
    

Where:

  • vars - is a shorthand to JMeterVariables class instance
  • sampler - shorthand to parent sampler implementation class instance, in this case - HTTPSamplerProxy

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Java and JMeter API from Beanshell scripts.

0
votes

Just use the Logic Controller - If Controller. It allows to define the if statement using your variables. So, you can perform your actions only in case all parameters are not equal to null: enter image description here I've defined one single User Defined Variable in this example. Jmeter sends HTTP request only if it has a value defined.