在curl数据有效载荷中不能使用shell变量

时间:2019-03-19 22:28:26

标签: git shell curl

给出以下shell脚本

#!/usr/bin/env bash
log=`git log discord-report..development --no-merges --decorate --format=format:'%C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an'`
echo "$log"

curl -i -H "Content-Type: application/json" -X POST --data "{ \"username\" : \"CoopR Development Info\", \"content\" : \"@everyone\n**Development** Branch has been updated.\nPlease pull the newest state!\n Changelog: '$log'  \" }" https://discordapp.com/api/webhooks/XXX/XXX

echo "$log"产生以下结果:

(36 minutes ago) chore(demo): add vr development mission - xetra11
(6 hours ago) refactor(tasks): move aar to tasks addon and use more cba notify funcs - xetra11
(2 days ago) refactor(tasks): temp test deactivation - xetra11
(3 days ago) feat(tasks): add report reduncancy check function - xetra11

整个脚本输出为:

$ ./test.sh
(38 minutes ago) chore(demo): add vr development mission - xetra11
(6 hours ago) refactor(tasks): move aar to tasks addon and use more cba notify funcs - xetra11
(2 days ago) refactor(tasks): temp test deactivation - xetra11
(3 days ago) feat(tasks): add report reduncancy check function - xetra11
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   512  100    58  100   454     66    518 --:--:-- --:--:-- --:--:--   585HTTP/2 400
date: Tue, 19 Mar 2019 22:27:24 GMT
content-type: application/json
content-length: 58
set-cookie: __cfduid=XXX; expires=Wed, 18-Mar-20 22:27:24 GMT; path=/; domain=.discordapp.com; HttpOnly
strict-transport-security: max-age=31536000; includeSubDomains
x-ratelimit-limit: 5
x-ratelimit-remaining: 4
x-ratelimit-reset: 1553034447
via: 1.1 google
alt-svc: clear
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 4ba2dfa01fbcc4a1-DUS

{"code": 50006, "message": "Cannot send an empty message"}

到目前为止,我已经尝试了所有方法,但是我无法通过curl post发送我的git日志结果。

我想要的

目前curl返回一个错误。我希望将$log的内容包含在-d的{​​{1}}选项中。

curl

这就是我定义atm的方式(我尝试了很多格式,但是没有用)

1 个答案:

答案 0 :(得分:1)

使用jq生成格式正确的JSON,其中包含bash中的任意内容。在您的用例中,可能看起来像这样:

log=$(git log discord-report..development --no-merges --decorate \
  --format=format:'%C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an')

preamble='@everyone
**Development** Branch has been updated.
Please pull the newest state!
 Changelog: '

json_payload=$(jq -nc --arg log "$log" --arg preamble "$preamble" '
{
  "username" : "CoopR Development Info",
  "content" : "\($preamble)\($log)"
}')

curl -i \
  -H "Content-Type: application/json" \
  -X POST \
  --data "$json_payload" \
  https://discordapp.com/api/webhooks/XXX/XXX

作为任何人都可以运行的测试,请考虑以下因素(自2019年3月19日起,语法突出显示已损坏,但代码可以正常工作):

#!/usr/bin/env bash
case $BASH_VERSION in ''|[0-3]*) echo "ERROR: Bash 4.0+ needed" >&2; exit 1;; esac

log=$(cat <<'EOF'

  This is a sample message with "quotes", both 'single' and "double" and 'un'matched'.

It also has a *, and a backslash: \ <- there.
EOF
)

json_payload=$(jq -nc --arg log "$log" --arg preamble "$preamble" '
{
  "username" : "CoopR Development Info",
  "content" : "\($preamble)\($log)"
}')

printf '%s\n' "$json_payload"

...正确发出:

{"username":"CoopR Development Info","content":"@everyone\n**Development** Branch has been updated.\nPlease pull the newest state!\n Changelog: \n  This is a sample message with \"quotes\", both 'single' and \"double\" and 'un'matched'.\n\nIt also has a *, and a backslash: \\ <- there."}