我正在尝试读取包含超过 500 行的 csv 文件,每一行都将作为对 API 的请求。现在我的问题是一些参数有空字符串,我想设置一个条件,以防参数返回空字符串,然后在点击 API 之前从请求正文中删除该参数
下面是我的json
{
"body": {
"Id1": "${Id1}",
"addressId": "${addressId}",
"languageCode": "${languageCode}",
"tempId": "${tempId}"
}
Run Code Online (Sandbox Code Playgroud)
现在在阅读 csv 后,我在我的请求正文中得到以下值
{
"body": {
"Id1": "1",
"addressId": "1233",
"languageCode": "E",
"tempId": ""
}
Run Code Online (Sandbox Code Playgroud)
如您所见, tempId 具有空字符串。现在使用 bean-shell 预处理器我试图删除它但没有运气
Object requestBody = sampler.getArguments().getArgument(0).getValue();
if (requestBody.get("tempId").equals("")){
sampler.getArguments.removeArgument("tempId");
}
Run Code Online (Sandbox Code Playgroud)
当我查看结果树时,我没有看到 tempId 从请求中删除。我将不胜感激任何帮助
避免使用 Beanshell 来导致弃用和性能不佳。
使用groovy代替以下代码:
import org.apache.jmeter.config.Arguments;
def request = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
def newRequest = evaluate(request.inspect())
request.body.each { entry ->
if (entry.getValue().equals('')) {
newRequest.body.remove(entry.getKey())
}
}
def arguments = new Arguments();
sampler.setArguments(arguments);
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(newRequest), '')
sampler.setPostBodyRaw(true)
Run Code Online (Sandbox Code Playgroud)
看:
如果你想正确学习jmeter,这本书会对你有所帮助。
归档时间: |
|
查看次数: |
889 次 |
最近记录: |