jmeter 如何发送内容类型为表单数据的请求?

ruoxqz4g  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(153)

问题是:如何发送具有内容类型表单数据的主体的请求,并且此请求主体应包含先前响应中保存的参数
代码为:

TestPlanStats stats = testPlan(
    threadGroup(1, 1),
             httpSampler(firstRequestUrl)
                    .method(POST)
                    .contentType(APPLICATION_JSON)
                    .header(AUTHORIZATION, getToken())
                    .body(someBody)
                    .children(new DslJsr223PostProcessor("Get param",
                    "def jsonResponse = new groovy.json.JsonSlurper().parseText(prev.getResponseDataAsString())\n" +
                            "def paramValue = jsonResponse.param\n" +
                            "vars.put('paramKey', paramValue)")),
            httpSampler(secondRequestUrl)
                    .method(POST)
                    .contentType(MULTIPART_FORM_DATA)
                    .body(here need to provide form body with "${paramKey}")
                    .children(new DslJsr223PostProcessor("Get response status code",
                            "int statusCode = prev.getResponseCode();\n" +
                                    "vars.put('responseStatusCode', String.valueOf(statusCode));"
                    )),
            new DslJsr223Sampler("Print result",
                    "String result = vars.get('responseStatusCode');" +
                            "if (result != null) {\n" +
                            "    println('csrfKey: ' + result);\n" +
                            "} else {\n" +
                            "    println('result not found');\n" +
                            "}")

    ).rampTo(testParams.getThreads(), Duration.ofMinutes(testParams.getRumpUpTime())),
    influxDbListener(envSetting.getInfluxDbUrl())
            .measurement("someMeasurement")
).run();

从第一个httpSampler()开始,我将参数保存到JMetervarsMap中。
在第二个httpSampler()中,我需要发送一个内容类型为Form-data的请求,该请求应包含保存在JMeter varsMap中的参数“${paramKey}”
请求体应包含参数:

  • param1 =“${paramKey}”
  • 参数2 = 10.01
  • param3 =“SomeText1”
  • param4 = false
  • param5.key1 = 11111
  • param5.key2 =“SomeText2”
  • param5.key3 = 22222

方法body(Function<PreProcessorVars, String> bodySupplier)body(String body)的签名。
如何用这些需求构建一个合适的请求体?

dgenwo3n

dgenwo3n1#

例如:

httpSampler(secondRequestUrl)
        .method(POST)
        .bodyPart("param1", "${paramKey}", TEXT_PLAIN)
        .bodyPart("param2", "10.01", TEXT_PLAIN)
        //etc
        .bodyFilePart("foo", "/path/to/your/file", APPLICATION_OCTET_STREAM)

将生成如下所示的HTTP请求采样器:

更多信息请参阅:

相关问题