【问题标题】:How to pass maps as array of parameters to Post request in JMeter如何将地图作为参数数组传递给 JMeter 中的 Post 请求
【发布时间】:2016-01-19 15:00:21
【问题描述】:

想要使用以下参数生成 Post 请求。
参数:
is_checked: false rc_notes: "test_notes" tags: [{id: 23, is_checked: false},{id:67, is_checked:true},{id: 63, is_checked: false},{id:16, is_checked:true}] theos: []

在上面,tagstheos 数组值将根据给定的输入动态变化。使用输入数据,JDBC 采样器将从 DB 中获取数据,并在此基础上构造 POST 请求参数。

已尝试使用 BeanShell PreProcessor 来构造 Post 调用的参数。由于参数不足,出现 400 错误

BeanShell 代码:
import java.util.ArrayList; import java.util.HashMap; import java.lang.Integer; import java.util.List; import java.util.Map;

//来自 JDBC 采样器的结果集计数 int count = Integer.parseInt(vars.get("tag_id_#")); 映射 tag_parameter_map = null; 列表 tag_param_list = new ArrayList(); 列出 theo_param_list = new ArrayList(); for(int i=1;i tag_parameter_map.put("is_checked", "false"); tag_param_list.add(tag_parameter_map); } sampler.addArgument("rc_notes", "notes"); sampler.addArgument("is_checked", "false"); sampler.addArgument("标签", tag_param_list); sampler.addArgument("theos", theo_param_list);

谁能指导我如何实现这一目标。

【问题讨论】:

    标签: java jmeter performance-testing beanshell


    【解决方案1】:

    您还需要提供 JSON 格式的参数和正确的类型(数字和布尔值)。为此,您将需要一些库,例如org.json(不要忘记在 jmeter 中包含 Jar)。所以它看起来像这样:

    JSONArray tag_param_list = new JSONArray(); // empty array
    JSONArray theo_param_list = new JSONArray(); // we will add elements in the loop
    for(int i=1;i<=count;i++) {
        // Construct object {id:X,is_checked:false} and add to array
        theo_param_list.put(new JSONObject()
            .put("id", Integer.parseInt(vars.get("tag_id_" + i)))
            .put("is_checked", false));
    }
    
    sampler.addArgument("tags", tag_param_list.toString());
    sampler.addArgument("theos", theo_param_list.toString());
    

    【讨论】:

    • 感谢您的回复。按照你说的试过,参数是这样传递的 rc_notes=notes&is_checked=false&tags=%5B%5D&theos=%5B%5D。收到 400 错误
    • @Manigandan:没错,它们是 URL 编码的(如预期的那样)。 %5B%5D 是 [] - 空 JSON 数组。其中一个应该是空的(标签)。但另一个应该填充在循环内。如果它仍然为空,则表示int count 为 0 或循环内有异常。尝试添加Debug Sampler并查看值count值,并查看日志中的异常。
    • 作为空数组显式传递。即使我作为空数组作为参数传递,我的 api 也会响应 200。但截至目前,我得到了 400。是否可以解码 URL 并查看实际传递的值。
    • 等一下,您的原始示例显示参数是用冒号指定的(即tags: ...)对吗?因为addArgument 默认情况下会使用=&amp; 分隔符添加它们。一般来说,最好检查一下您的服务器端期望参数如何呈现。因为现在数据是正确的,但是格式可能不正确。
    • 服务器需要问题中提到的冒号格式的参数。
    【解决方案2】:

    与 OP 讨论后,似乎要求是:

    • JSON 编码参数
    • 名称/值分隔符是:(而不是通常的=
    • 参数分隔符是换行符(而不是通常的&amp;

    有了这组要求,addArgument 的使用是不可能的。相反,我提出以下解决方案:

    您仍然需要相同的预处理器,代码几乎相同,但不要将值作为参数添加到 HTTP 请求,而是将它们保存为变量:

    JSONArray tag_param_list = new JSONArray(); // empty array
    JSONArray theo_param_list = new JSONArray(); // we will add elements in the loop
    for(int i=1;i<=count;i++) {
        // Construct object {id:X,is_checked:false} and add to array
        theo_param_list.put(new JSONObject()
            .put("id", Integer.parseInt(vars.get("tag_id_" + i)))
            .put("is_checked", false));
    }
    vars.put("tags", tag_param_list.toString());
    vars.put("theos", theo_param_list.toString());
    vars.put("rc_notes", "notes");
    vars.put("is_checked", "false");
    

    现在在 HTTP 请求中,切换到“正文数据”选项卡(而不是“参数”选项卡)并指定原始正文数据,如下所示:

    rc_notes: ${rc_notes}
    is_checked: ${is_checked}
    theos: ${theos}
    tags: ${tags}
    

    这将产生这样的帖子数据:

    POST data:
    rc_notes: notes
    is_checked: false
    theos: [{"is_checked":false,"id":1},...]
    tags: []
    

    这看起来就像原始海报想要的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2018-07-02
      • 2022-11-18
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      相关资源
      最近更新 更多