json - 我如何将一个参数发送到rest api,它可以为空或有一些值?
问题描述
我如何将一个参数发送到rest api,它可以为空或有一些值?我想用json发送这个参数。数据是从外部文件中读取的,我的问题是该文件有几行,在其中一些行中,我的参数值是空的,有些包含数据我想自动删除自动测试中为空的参数,甚至删除键直到没有发生异常。
"length": "${DataSource#Length}",
"offset": "${DataSource#Offset}",
"cityCode": "${DataSource#CityCode}",
"cityName" : "${DataSource#CityName}" ,
"provinceCode": "${DataSource#ProvinceCode}",
"provinceName" : "${DataSource#ProvinceName}"
解决方案
您可以在运行 API 之前编写一个groovy 步骤,您将在其中根据变量中的空值准备输入
逻辑:-
- 从数据源中提取值
- 检查它们是否为空不要将它们添加到输入中
- 所以最终准备好的字符串存储在测试用例属性中
- 该属性可以在实际的休息请求中扩展
`
String jsonInput=""
def Length=context.expand("${DataSource#Length}")
def offset=context.expand("${DataSource#Offset}")
if(Length)
{
jsonInput=jsonInput+Length
}
if(offset!=null)
{
jsonInput=jsonInput+offset
}
testRunner.testCase.setPropertyValue("jsonInput",jsonInput)
现在在 Rest 步骤中,您可以提及
${#TestCase#jsonInput}
推荐阅读
- amazon-web-services - AWS 经典负载均衡器侦听器未创建,然后消失
- javascript - 在 setInterval 函数中传递计数变量
- ruby-on-rails - Ransack Select 有两个选项:真或空/假
- reactjs - 等待多个可观察的 ajax 调用完成
- .htaccess - 如何使用 .htaccess 减少重定向链?
- python - Python - Scrapy 到 Json 输出拆分
- c# - 如何对零和一的字符串数组进行排序?
- magento - 我想覆盖产品视图 phtml 页面,我在 stackoverflow 和某处阅读了所有解决方案,没有找到任何更改或覆盖
- html - 从 RGBA 背景过渡线性渐变背景
- javascript - django模板调用js对象