Jmeter 上的 post 方法的 beanShell json body

beanShell json body for post method on Jmeter

任何人都可以帮助在这个儿子上添加带斜线的转义引号,例如 \" body:

    {
  "firstName": "teo",
  "lastName": "leo",
  "companyName": "abc",
  "restaurantId": "54d34443e4b0382b3208703d",
  "phones": [
    {
      "label": "Mobile",
      "value": "123456789",
      "countryCode": "+123",
      "isPrimary": true
    }
  ],
  "addresses": "haha"
}

我试过这个但是beanShell PreProcessor不能接受

String formvalues = "{\"firstName\": \"teo\",\"lastName\": \"leo\",\"companyName\": \"abc\",\"restaurantId\": \"54d34443e4b0382b3208703d\",\"phones\": [{\"label\":\"Mobile\",\"value\": \"123456789\",\"countryCode\": \"+123\",\"isPrimary\": true}],\"addresses\": \"haha\"}"

非常感谢!

如果要继续格式化:

String formvalues = "{\n" +
        "  \"firstName\": \"teo\",\n" +
        "  \"lastName\": \"leo\",\n" +
        "  \"companyName\": \"abc\",\n" +
        "  \"restaurantId\": \"54d34443e4b0382b3208703d\",\n" +
        "  \"phones\": [\n" +
        "    {\n" +
        "      \"label\": \"Mobile\",\n" +
        "      \"value\": \"123456789\",\n" +
        "      \"countryCode\": \"+123\",\n" +
        "      \"isPrimary\": true\n" +
        "    }\n" +
        "  ],\n" +
        "  \"addresses\": \"haha\"\n" +
        "}";

如果你想要单行(注意 Content-Length 会有所不同)

String formvalues = "{\"firstName\":\"teo\",\"lastName\":\"leo\",\"companyName\":\"abc\",\"restaurantId\":\"54d34443e4b0382b3208703d\",\"phones\":[{\"label\":\"Mobile\",\"value\":\"123456789\",\"countryCode\":\"+123\",\"isPrimary\":true}],\"addresses\":\"haha\"}";

生成主体并将其添加为参数的完整代码:

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;

String formvalues = "{\n" +
           "  \"firstName\": \"teo\",\n" +
           "  \"lastName\": \"leo\",\n" +
           "  \"companyName\": \"abc\",\n" +
           "  \"restaurantId\": \"54d34443e4b0382b3208703d\",\n" +
           "  \"phones\": [\n" +
           "    {\n" +
           "      \"label\": \"Mobile\",\n" +
           "      \"value\": \"123456789\",\n" +
           "      \"countryCode\": \"+123\",\n" +
           "      \"isPrimary\": true\n" +
           "    }\n" +
           "  ],\n" +
           "  \"addresses\": \"haha\"\n" +
           "}";

Arguments arguments = new Arguments();
arguments.addArgument(new HTTPArgument("",formvalues));       
sampler.setArguments(arguments);

JavaDoc 相关 类:

有关 JMeter 中 Beanshell 脚本的更多信息,请参阅 How to Use BeanShell: JMeter's Favorite Built-in Component 指南。