JMeter-如果为null,则不要在发布数据中传递发布值 [英] JMeter - do not pass post value in post data if null
问题描述
在我的JMeter脚本中,我有一个HTTP请求,该请求有4个不同的参数要在帖子正文中传递.我有相应的变量.这些变量的值并非每次都可用,具体取决于配置.
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.
如果没有可用的值,我将收到一个错误的错误请求".我如何查看变量是否不为null,然后才在请求发布正文中传递相应的参数?
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?
推荐答案
鉴于您具有以下配置:
,如果未定义${bar}
变量,则不想发送foo
参数
and you don't want to send foo
parameter if ${bar}
variable is not defined
- 将 Beanshell预处理器添加为HTTP请求采样器的子代
-
将以下代码放入预处理器的脚本"区域:
- Add Beanshell PreProcessor as a child of your HTTP Request Sampler
Put the following code into the PreProcessor's "Script" area:
if (vars.get("bar") == null) {
sampler.getArguments().removeArgument("foo");
}
位置:
-
vars
-是 JMeterVariables的缩写类实例 -
sampler
-父采样器实现类实例的简写,在这种情况下-
vars
- is a shorthand to JMeterVariables class instancesampler
- shorthand to parent sampler implementation class instance, in this case - HTTPSamplerProxy
请参见如何使用BeanShell:JMeter最喜欢的内置组件指南,以获取有关通过Beanshell脚本使用Java和JMeter API的更多信息.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Java and JMeter API from Beanshell scripts.
这篇关于JMeter-如果为null,则不要在发布数据中传递发布值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!