2016-12-21 24 views
1

我必須通過此字符串StringBody我加特林要求如何將Stringbody中的json字符串與Gatling中的動態值一起傳遞?

{"definitionId":4891544, "stringVariables" : {"Copy 1-json-metadata-instance":" {\r\n \"external-resource\": {\r\n \"id\": 4881561,\r\n \"name\": \"Training Outbound-File\",\r\n \"type\": \"resource\"\r\n },\r\n \"asset-context\": {\r\n \"value\": \".\",\r\n \"isExpression\": false\r\n },\r\n \"vfs-location\": {\r\n \"protocol\": \"FILE\",\r\n \"port\": 0,\r\n \"path\": \"/fx/fx-enterprise/storage/media/Training Outbound-FILE\"\r\n }", "Copy 2-json-metadata-instance":" {\r\n \"external-resource\": {\r\n \"id\": 48218841,\r\n \"name\": \"Training Outbound-File\",\r\n \"type\": \"resource\"\r\n },\r\n \"asset-context\": {\r\n \"value\": \".\",\r\n \"isExpression\": false\r\n },\r\n \"vfs-location\": {\r\n \"protocol\": \"FTP\",\r\n \"hostname\": \"ntvt.upload.llnw.net\",\r\n \"port\": 0,\r\n \"path\": \"/poc/V5Dev/R1\",\r\n \"username\": \"ntvt-ht-nt-staging\",\r\n \"password\": \"jpc6t8\"\r\n }"} }}} 

這裏是我的斯卡拉腳本

// Dynamic Json values 
    val filePath = System.getProperty("filePath", "/fx/fx-enterprise/storage/media/Training Outbound-FILE") 
    val ftpHostname = System.getProperty("ftpHostname", "ntvt.upload.llnw.net") 
    val ftpUsername = System.getProperty("ftpUsername", "ntvt-ht-nt-staging") 
    val ftpPassword = System.getProperty("ftpPassword", "jpc6t8") 
    val ftpPath = System.getProperty("ftpPath", "/poc/V5Dev/R1") 
    val UUID = System.getProperty("UUID", "b71awerv-4946-4bwe-8eqa-2412fgh98002") 
    val definitionId = System.getProperty("definitionId", "48924212") 

    val scn = scenario("Launch") 
      .exec(http("Wf") 
      .post("""/api/workflows""") 
      .headers(headers_0) 
    // Modification-1 
     .body(StringBody(s"""{"definitionId":"${definitionId}", "stringVariables" : { "Copy 1-json-metadata-instance":" {\r\n \"external-resource\": {\r\n \"id\": 488841,\r\n \"name\": \"Training Outbound-File\",\r\n \"type\": \"resource\"\r\n },\r\n \"asset-context\": {\r\n \"value\": \".\",\r\n \"isExpression\": false\r\n },\r\n \"vfs-location\": {\r\n \"protocol\": \"FILE\",\r\n \"port\": 0,\r\n \"path\": \"${filePath}\"\r\n }", "Copy 2-json-metadata-instance":" {\r\n \"external-resource\": {\r\n \"id\": 488841,\r\n \"name\": \"Training Outbound-File\",\r\n \"type\": \"resource\"\r\n },\r\n \"asset-context\": {\r\n \"value\": \".\",\r\n \"isExpression\": false\r\n },\r\n \"vfs-location\": {\r\n \"protocol\": \"FTP\",\r\n \"hostname\": \"${ftpHostname}\",\r\n \"port\": 0,\r\n \"path\": \"${ftpPath}\",\r\n \"username\": \"${ftpUsername}\",\r\n \"password\": \"${ftpPassword}\"\r\n }"} } }}""")).asJSON) 

    //Modification-2 
     // .body(StringBody(s"""{"definitionId":"$definitionId", "stringVariables" : { "Copy 1-json-metadata-instance":" {"external-resource": {"id": 488841,"name": "Training Outbound-File", "type": "resource"},"asset-context": {"value": ".","isExpression": false},"vfs-location": {"protocol": "FILE","port": 0,"path": "$filePath"}", "Copy 2-json-metadata-instance":" {"external-resource": {"id": 488841,"name": "Training Outbound-File","type": "resource"},"asset-context": {"value": ".","isExpression": false},"vfs-location": {"protocol": "FTP","hostname": "$ftpHostname","port": 0,"path": "$ftpPath","username": "$ftpUsername","password": "$ftpPassword"}"} } }}"""))) 

    setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol) 

它的工作原理,如果我通過,因爲它是沒有動態值原始字符串) 下方拋出錯誤:

====輸出與變形例1 ====

compositeByteData={"definitionId":"4891544", "stringVariables" : { "Copy 1-json-metadata-instance":" 
{ 
"external-resource": { 
"id": 488841, 
"name": "Training Outbound-File", 
"type": "resource" 
}, 
"asset-context": { 
"value": ".", 
"isExpression": false 
}, 
"vfs-location": { 
"protocol": "FILE", 
"port": 0, 
"path": "/fx/fx-enterprise/storage/media/Training Outbound-FILE" 
}", "Copy 2-json-metadata-instance":" { 
"external-resource": { 
"id": 488841, 
"name": "Training Outbound-File", 
"type": "resource" 
}, 
"asset-context": { 
"value": ".", 
"isExpression": false 
}, 
"vfs-location": { 
"protocol": "FTP", 
"hostname": "ntvt.upload.llnw.net", 
"port": 0, 
"path": "/poc/V5Dev/R1", 
"username": "ntvt-ht-nt-staging", 
"password": "jpc6t8" 
}"} } }} 
realm=Realm{principal='masteruser', password='masteruser', scheme=BASIC, realmName='null', nonce='n 
ull', algorithm='null', response='null', qop='null', nc='00000001', cnonce='null', uri='null', meth 
odName='GET', useAbsoluteURI='false', omitQuery='false'} 
========================= 
HTTP response: 
status= 
500 Internal Server Error 
headers= 
Content-Type: [application/json] 
Date: [Tue, 20 Dec 2016 10:41:29 GMT] 
Server: [Apache-Coyote/1.1] 
Content-Length: [69] 
Connection: [keep-alive] 

body= 
{ 
    "errors" : { 
    "error" : "Error while processing request" 
    } 
} 

修改例2 ====

body= 
{ 
    "errors" : { 
    "error" : "Unexpected character ('e' (code 101)): was expecting comma to separate OBJECT entries\n at [Source: [email protected]; line: 1, column: 86]" 
    } 
} 

回答

0

====輸出當你在斯卡拉任何String前加s你調用一個串插。根據其文檔,您必須刪除所有反斜槓,因爲它們會導致InvalidEscapeException。

/** The simple string interpolator. 
    * 
    * It inserts its arguments between corresponding parts of the string context. 
    * It also treats standard escape sequences as defined in the Scala specification. 
    * Here's an example of usage: 
    * {{{ 
    * val name = "James" 
    * println(s"Hello, $name") // Hello, James 
    * }}} 
    * In this example, the expression $name is replaced with the `toString` of the 
    * variable `name`. 
    * The `s` interpolator can take the `toString` of any arbitrary expression within 
    * a `${}` block, for example: 
    * {{{ 
    * println(s"1 + 1 = ${1 + 1}") 
    * }}} 
    * will print the string `1 + 1 = 2`. 
    * 
    * @param `args` The arguments to be inserted into the resulting string. 
    * @throws IllegalArgumentException 
    *   if the number of `parts` in the enclosing `StringContext` does not exceed 
    *   the number of arguments `arg` by exactly 1. 
    * @throws StringContext.InvalidEscapeException 
    *   if a `parts` string contains a backslash (`\`) character 
    *   that does not start a valid escape sequence. 
    */ 
    def s(args: Any*): String = standardInterpolator(treatEscapes, args) 

我建議刪除所有「\」 也替代「與」

我希望這將有助於

+0

嗨亞歷克斯,我從代碼中刪除所有的「/」和「/ R/N」和「與」同,但‘錯誤更換:在處理請求‘ – Peter

+0

感謝您的回答亞歷克斯錯誤’’。它與\\而不是\ – Peter

3

我格式化JSON字符串的身體時,發現在JSON對象開始的地方有行情我不確定你的JSON是否有效enter image description here

1

刪除\ r \ n並添加了額外的反斜槓\ \(我的意思是使用雙反斜槓\而不是\單反斜槓)

.body(StringBody(s"""{"definitionId":${definitionId}, "stringVariables" : { "Copy 1-json-instance":" { \\"external-resource\\": { \\"id\\": 48234231, \\"name\\": \\"Outbound-File\\", \\"type\\": \\"resource\\" }, \\"asset-context\\": { \\"value\\": \\".\\", \\"isExpression\\": false }, \\"location\\": { \\"protocol\\": \\"FILE\\", \\"port\\": 0, \\"path\\": \\"${filePath}\\" }", "Copy 2-json-instance":" { \\"external-resource\\": { \\"id\\": 4823441, \\"name\\": \\"Outbound-File\\", \\"type\\": \\"resource\\" }, \\"asset-context\\": { \\"value\\": \\".\\", \\"isExpression\\": false }, \\"location\\": { \\"protocol\\": \\"FTP\\", \\"hostname\\": \\"${ftpHostname}\\", \\"port\\": 0, \\"path\\": \\"${ftpPath}\\", \\"username\\": \\"${ftpUsername}\\", \\"password\\": \\"${ftpPassword}\\" }"} }"""))) 
相關問題