Bash 如何在POST cURL请求JSON数据中插入另一个JSON?
我的Bash 如何在POST cURL请求JSON数据中插入另一个JSON?,bash,curl,escaping,Bash,Curl,Escaping,我的报告json如下所示: { "passed": 11, "total": 11, "collected": 11 } 我定义了一个变量测试报告,并将上面的值赋给它。但无法在curl命令中使用它?我将错误视为无效的\u负载gcloud构建日志 - name: gcr.io/cloud-builders/gcloud:latest entrypoint: 'bash' dir: 'workspace' env:
报告
json如下所示:
{
"passed": 11,
"total": 11,
"collected": 11
}
我定义了一个变量测试报告
,并将上面的值赋给它。但无法在curl命令中使用它?我将错误视为无效的\u负载
gcloud构建日志
- name: gcr.io/cloud-builders/gcloud:latest
entrypoint: 'bash'
dir: 'workspace'
env:
- 'TEST_REPORT=${_TEST_REPORT}'
args:
- '-c'
- |
cd /workspace
#Installing jq
apt-get update -y && apt-get install -y curl unzip groff jq bc less
#testing jq is successfully installed or not
jq --help
cat mezi_automation_test_report.json
#assgined summary object to test_report file
jq .summary mezi_automation_test_report.json > test_report
#check test_report file is displayed with summary object or not
cat test_report
report=$(cat test_report)
#check report variable has summary object or not
echo report
curl --location --request POST '$_SLACK_WEBHOOK' \
--header 'Content-Type: application/json' \
--data-raw '{
"channel":"$_CHANNEL",
"username":"Cloudbuild",
"icon_url":"$_ICON_URL",
"text":"Test Report",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": " Test Report *`$_BRANCH`* branch on QA"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Version: *$_VERSION*\n TEST_REPORT: *"${report}"*"
}
}
]
}'
“
单引号内的任何内容都不能解释为正则表达式,因此必须先关闭它,然后使用任何bash命令/变量。之后,您可以再次启动单引号以附加字符串/json的其余部分
注意:如果bash命令有一个特殊的输出,如json值,请将其括在双引号中“
或者,如果您想要更多的控制,请分别解析每个数据值
passed=$(jq .summary.passed mezi_automation_test_report.json)
total=$(jq .summary.total mezi_automation_test_report.json)
collected=$(jq .summary.collected mezi_automation_test_report.json)
curl "http://localhost/send" -d '{"text":"Test Report: \nPassed: '$passed' \nTotal: '$total' \nCollected: '$collected' \nEnd of Report" } '
实际上,问题应该是,如何在curl数据中插入JSON或运行时变量!这与google cloud build或build Triggersys无关,你是对的,我认为这是关于google cloud build的,因为我正在使用它进行构建、部署和测试。工作正常!为total定义变量,收集并传递它们,并将它们添加到curl数据中下面:
“text:”Version:$\u Version*\n TEST\u REPORT:*运行的总测试数:'$Total\u Tests'*\n*收集的测试数:'$Collected\u Tests'*\n*通过的测试数:'$Passed\u Tests'*\n*失败的测试数:“
非常感谢@s4p3r\u c1n0s,节省了我的时间,非常感谢。
passed=$(jq .summary.passed mezi_automation_test_report.json)
total=$(jq .summary.total mezi_automation_test_report.json)
collected=$(jq .summary.collected mezi_automation_test_report.json)
curl "http://localhost/send" -d '{"text":"Test Report: \nPassed: '$passed' \nTotal: '$total' \nCollected: '$collected' \nEnd of Report" } '