访问保存在冒号变量中的HTTP响应数据



我使用谷歌云工作流通过http调用。获得一个CloudRun应用程序,返回一个已转换为json的XML文档,下面的json在步骤2中成功返回到Workflow,其中包含已转换为json的XML。

{
"body": {
"ResponseMessage": {
"@xmlns": "http://someurl.com/services",
"Response": {
"@xmlns:a": "http://someurl.com/data",
"@xmlns:i": "http://www.w3.org/2001/XMLSchema-instance",
"a:ReferenceNumber": {
"@i:nil": "true"
},
"a:DateTime": "2023-01-01T00:17:38+0000",
"a:TransactionId": "154200432",
"a:Environment": "Development",
"a:RequestDateTime": "2023-01-01T11:17:39",            
}
},
"code": 200,
"headers": {
"Alt-Svc": "h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"",
"Content-Length": "1601",
"Content-Type": "application/json",
"Date": "Sun, 01 Jan 2023 00:17:39 GMT",
"Server": "Google Frontend",
"X-Cloud-Trace-Context": "931754ab82102397eb07775171485850"
}
}
}

工作流的完整文本如下,没有步骤3/步骤4它可以工作。在步骤3中,我尝试访问从步骤2返回的json中的元素,如https://cloud.google.com/workflows/docs/http-requests#access-data

main:
params: [args]
steps:
- step1:
assign:
- searchURL: ${"https://myfunction.a.run.app/search/" + args.type + "/" + args.serial}
- step2:
call: http.get
args:
url: ${searchURL}
result: s2result
- step3:
assign:       
- resubmitURL: '${https://myfunction.a.run.app/resubmit/" + ${s2result.body.ResponseMessage.Response.a:TransactionId} }'
- step4:
call: http.get
args:
url: ${resubmitURL}
result: s4result
- returnOutput:
return: ${s4result}

然而,由于冒号:在我试图访问的字段中,当我试图保存另一个变量分配时,有yaml解析错误。当属性字段中有冒号时,如何访问保存在变量中的HTTP响应数据?

控制台的错误也相似

Could not deploy workflow: main.yaml:14:25: parse error: in workflow 'main', step 'step3': token recognition error at: ':'
- resubmitURL: '${"https://myfunction.a.run.app/resubmit/" + ${s2result.body.ResponseMessage.Response.a:TransactionId}'
^
main.yaml:14:25: parse error: in workflow 'main', step 'step3': mismatched input '+' expecting {'[', LOGICAL_NOT, '(', '-', TRUE, FALSE, NULL, NUM_FLOAT, NUM_INT, STRING, IDENTIFIER}
- resubmitURL: '${"https://myfunction.a.run.app/resubmit/" + ${s2result.body.ResponseMessage.Response.a:TransactionId}'

使用如下特殊字符引用映射键需要两种技巧:

  1. 根据文档中的建议,所有表达式都应该用单引号包装,以避免YAML解析错误(即'${...}')。
  2. 当使用特殊字符引用键时,您可以使用数组符号将键名用引号括起来(例如var["KEY"])。

合起来是这样的:

main:
steps:
- init:
assign:
- var:
key: 
"co:lon": bar
- returnOutput:
return: '${"foo" + var.key["co:lon"]}'

在你的代码中,你正在使用表达式内部的表达式:

- resubmitURL: '**${**"https://myfunction.a.run.app/resubmit/" + **${**s2result.body.ResponseMessage.Response.a:TransactionId**}**'

在这个错误信息示例中,你甚至没有正确关闭表达式。

如果您将所有内容打包到一个表达式中,并使用Kris的提示和键,它应该部署:

- resubmitURL: '${"https://myfunction.a.run.app/resubmit/" + s2result.body.ResponseMessage.Response["a:TransactionId"]}'

下面是我的完整测试用例:

main:
params: [args]
steps:
- init_assign:
assign:
- input: ${args}
- s2result:
body:
ResponseMessage:
Response:
"a:TransactionId": "Test"
- resubmitURL: '${"https://myfunction.a.run.app/resubmit/" + s2result.body.ResponseMessage.Response["a:TransactionId"]}'
- log1:
call: sys.log
args:
text: ${resubmitURL}
severity: INFO

附带日志:'https://myfunction.a.run.app/resubmit/Test'

最新更新