本文介绍了如何在scala中使用for循环创建动态json以传递Gatling post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to scala ,Any help would be very much appreciated

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

I have a scala Map which has around 10 ids and 10 weightsMap(id->id,weight->weight)

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

For post request in gatling , i would like to pass my request on basis of for loopExpected json: so for example , if there are 10 key values pair in my map - i would like to add 10 inner array in my final json string and there should be a single request.

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
     }

    ]

推荐答案

Gatling 提供了 Pebble 模板,可以定义和填充你的身体结构

Gatling provides Pebble Templates which can define and fill your body structure

有一个工作原理示例:https://gatling.io/2018/11/gatling-3-pebble-templating/

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

In your case body will be likes:

.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"),
  )))

这篇关于如何在scala中使用for循环创建动态json以传递Gatling post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:10