2017-02-24 46 views
0

我已经使用beanshell脚本解码了一个响应并将其保存在一个XML文件中。需要从Json文件发送一个属性作为http请求的输入

BeanShell的后置处理程序脚本

import org.apache.commons.io.FileUtils; 
import org.apache.commons.codec.binary.Base64; 

String response= vars.get("data"); 
vars.put("response",new String(Base64.decodeBase64(response))); 

Output = vars.get("response"); 

f = new FileOutputStream("E:/JMeter/apache-jmeter-3.1/bin/response.xml"); 
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print(Output); 
f.close(); 

响应

{ 
    "Username": "[email protected]", 
    "UserCreateDate": "2017-02-23T04:02:59.123Z", 
    "UserLastModifiedDate": "2017-02-23T04:04:50.695Z", 
    "Enabled": true, 
    "UserStatus": "CONFIRMED", 
    "sub": "50e0dff7-0342-40f1-b793-68de404d95c4", 
    "_app": "586cc7cc851dc874ac59f001", 
    "email_verified": "true", 
    "_org": "58adbaf2ffc4de735f1fbe28", 
    "name": "Raghu Ram kota", 
    "_groups": [ 
    { 
     "_id": "58adbaf2ffc4de735f1fbe29", 
     "name": "default", 
     "slug": "default", 
     "description": "Default Group", 
     "id": "58adbaf2ffc4de735f1fbe29" 
    } 
    ], 
    "_roles": [ 
    { 
     "_id": "58adbaf2ffc4de735f1fbe2a", 
     "name": "ADMIN", 
     "slug": "admin", 
     "description": "ADMIN Role", 
     "id": "58adbaf2ffc4de735f1fbe2a" 
    } 
    ], 
    "permissions": { 
    "listen": { 
     "DELETE": false, 
     "READ": true, 
     "UPDATE": false, 
     "CREATE": false 
    }, 
    "profile": { 
     "STOPLISTEN": true, 
     "STARTLISTEN": true, 
     "AUTHORIZE": true, 
     "DELETE": true, 
     "READ": true, 
     "UPDATE": true, 
     "CREATE": true 
    }, 
    "post": { 
     "DELETE": true, 
     "READ": true, 
     "UPDATE": true, 
     "CREATE": true 
    }, 
    "group": { 
     "DELETE": true, 
     "READ": true, 
     "UPDATE": true, 
     "CREATE": true 
    }, 
    "user": { 
     "DELETE": true, 
     "READ": true, 
     "UPDATE": true, 
     "CREATE": true 
    } 
    }, 
    "email": "[email protected]", 
    "meta": {}, 
    "isAdmin": "false", 
    "iat": 1487919175, 
    "exp": 1487926375 
} 

现在我需要只发送_org价值,这是强调,作为输入到另一个HTTP请求。有什么建议么?

回答

0

我建议切换到JSR223 PostProcessorGroovy语言:

所以,你可以得到你的_org值那样简单:

def response = vars.get("data") 

def decodedResponse = new String(response.decodeBase64()) 

def json = new groovy.json.JsonSlurper().parseText(decodedResponse) 

log.info(json._org) 

JSON Groovy Base64 JMeter

Groovy Is the New Black文章更全面的信息。

+0

谢谢,但我们怎么能发送这个作为输入到另一个http请求。我用vars.put(“CompId”,json_org)替换了log.info(json_org),并将其作为$ {CompId}在另一个hhtp请求中使用。我错过了什么。 –

+0

只需将其存储到像vars.put(“org”,json._org)这样的JMeter变量中,稍后根据需要将其引用为“$ {org}” –

+0

谢谢。它的工作现在。 –