使用 groovy 从 SOAPUI 中的 JSON 内容解码 base64 字符串

Decoding a base64 string from JSON content in SOAPUI using groovy

我从 REST api 收到 json 响应,如下所示

{
  "parentnode1":    {
  "childnode1": "abc12345-123-1234-1234-64a0251575f9",
  "childnode2": "VAL1",
  "childnode3": "format/pdf",
  "childnode4": "name.pdf",
  "base64content": "JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwRU9G"},
  "messages": "This is a message"
}

我想解码"base64content"的值,然后将其转换成pdf文件并保存到本地目录。这在 SOAP UI 和 Groovy 中可能吗?

我想出了这个 groovy 脚本,但没有在 SOAPUI 中测试它(从未使用过)。试试这个,让我知道会发生什么:

def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText (json_response)

String content_decoded = object.base64content.decodeBase64()

def file = new java.io.File("output.pdf")
    
FileOutputStream fos = new java.io.FileOutputStream(file)
    
fos.write( content_decoded )
    
fos.flush()
    
fos.close()