2016-01-13 32 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

相關問題