【问题标题】:How to create dynamic json using for loop in scala to pass in Gatling post request如何在scala中使用for循环创建动态json以传递Gatling post请求
【发布时间】:2021-09-11 20:49:33
【问题描述】:

我是 scala 新手,非常感谢任何帮助

我有一个 scala 地图,它有大约 10 个 ID 和 10 个权重 地图(id->id,weight->weight)

对于 gatling 中的 post 请求,我想基于 for 循环传递我的请求 预期的 json:例如,如果我的地图中有 10 个键值对 - 我想在我的最终 json 字符串中添加 10 个内部数组,并且应该有一个请求。

myjson=

of[
     {
      "id":pis
      "weight":20.3
     }
  ]

预期的 json=

of[
     {
      "id":pis
      "weight":20.3
     },
    {
      "id": 2nd value from for loop
      "weight":2nd value from for loop
     },
    {
      "id": 3rd value from for loop
      "weight":3rd value from for loop
     }

    ]

【问题讨论】:

    标签: json scala gatling scala-gatling


    【解决方案1】:

    Gatling 提供Pebble Templates 可以定义和填充你的身体结构

    有一个例子:https://gatling.io/2018/11/gatling-3-pebble-templating/

    在你的情况下,身体会喜欢:

    .body(PebbleStringBody(
          """{
            |  "of": [ {% for value in mapValues %}
            |    {
            |      "id": "{{value.id}}",
            |      "weight": "{{value.weight}}
            |    }{% if loop.last %}{% else %},{% endif %}
            |    {% endfor %}
            |  ]
            |}
            |""".stripMargin))
    

    馈线:

    val mapValuesFeeder = Iterator.continually(Map("mapValues" -> List(
        Map("id" -> "1", "weight" -> "10"),
        Map("id" -> "2", "weight" -> "20"),
      )))
    
    

    【讨论】:

    • 感谢您的回答,能否请您告诉 mw 地图的外观 - 我正在尝试使用馈线地图,但它不起作用。
    • @surbhikhare 我在回答中添加了示例。
    • 非常感谢@Amerousful,它对我有用:-)
    • 你知道如何用 csv 数据 Csv 填充这张地图
    • 对不起,这是一个不同的问题,但试试我的运气:)
    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2023-04-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2021-05-06
    相关资源
    最近更新 更多