2016-01-13 83 views
0

post方法谁能帮助这个儿子身上添加具有类似\反斜杠引号”:BeanShell的JSON身体上的JMeter

{ 
    "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\"}" 

感谢你这么多

+0

然后用'\“替换''''然后传递给beanShell –

回答

0

如果你想保持格式:

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" + 
     "}"; 

如果你想单行(介意,内容长度会有所不同)

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上相关分类:

How to Use BeanShell: JMeter's Favorite Built-in Component参见指南在JMeter的BeanShell的脚本的详细信息。

+0

哇,非常感谢你,我已经为http post方法生成了正确的json主体_”如果你想单行(介意Content - 长度将不同)“_ 将尝试其他你建议的:D – Chuoi

相关问题