【问题标题】:jMeter: load multiple rows from CSV file into HTTP bodyjMeter:将多行从 CSV 文件加载到 HTTP 正文中
【发布时间】:2020-03-03 23:41:35
【问题描述】:

我正在尝试向接受项目列表的服务构建 HTTP 请求,例如:

{
    "user": "john", 
    "table": "goods",
    "articles": [ 
        {"id": "003", "quantity": 1},
        {"id": "004", "quantity": 1},
        {"id": "023", "quantity": 2},
        {"id": "011", "quantity": 3},
        {"id": "063", "quantity": 1},
        {"id": "006", "quantity": 7}
    ]
}

我的目标是从 CSV 文件加载所有文章,如下所示:

我想要类似的东西:

{
    "user": "john", 
    "table": "goods",
    "articles": [ 
        {"id": "${id}", "quantity": ${qte}}
    ]
}

谁能帮帮我?



更新:

Dmitri T 解决了我的问题,谢谢! 我还问他如何将 JSON 对象(字典)添加到我的身体,而他回答我找到了一个解决方案,如果有人需要,我将在这里分享。如果要添加:

"user": {"id": 1, "name": "John"}

到你的正文内容,你只需要这样做:

def user = [:]

//populate user
user.put('id', 1)
user.put('name', 'John')

//add user entity to body content
content.put("user", user)

玩得开心!

【问题讨论】:

    标签: jmeter


    【解决方案1】:

    您将无法获得上面提供的类似内容,动态构建 JSON 请求正文的唯一方法是使用 JSR223 PreProcessor 以编程方式构建它

    1. 将 JSR223 PreProcessor 添加为要参数化哪个主体的请求的子项
    2. 将以下代码放入“脚本”区域:

      def content = [:]
      content.put('user', 'john')
      content.put('table', 'goods')
      def articles = []
      new File('test.csv').readLines().each { line ->
          def article = [:]
          article.put('id', line.split(',')[0])
          article.put('quantity', line.split(',')[1])
          articles.add(article)
      }
      content.put('articles', articles)
      sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(content).toPrettyString(), '')
      sampler.setPostBodyRaw(true)
      
    3. 就是这样,当您运行测试时,预处理器将从 CSV 文件生成请求正文并将其添加到运行中的HTTP Request 采样器。

    参考资料:

    【讨论】:

    • 您好 Dmitri T,感谢您的回答。我认为这正是我正在寻找的。您能否告诉我如何直接将 JSON 实体添加到脚本中?取而代之的是:“user”:“john”我会添加:“user”:{“id”:1,“name”:“John”}
    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多