如何发送content-type为form-data的请求?

问题描述 投票:0回答:1

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

代码是:

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() 开始,我将参数保存到 JMeter vars 映射中。

在第二个 httpSampler() 中,我需要发送一个内容类型为 Form-data 的请求,该请求应包含保存在 JMeter vars 映射参数“${paramKey}”中的内容

请求体应包含参数:

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

方法的签名

body(Function<PreProcessorVars, String> bodySupplier)
body(String body)

如何根据这些要求构建适当的请求正文?

java jmeter dsl
1个回答
0
投票

类似:

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 请求采样器:

更多信息:

© www.soinside.com 2019 - 2024. All rights reserved.