如何在 bash 上进行字符串连接并使用curl 将其作为 json 发送?

问题描述 投票:0回答:1

这就是我正在努力实现的目标:

body="\"\:warning\: \:exclamation\: Your pull request is not linked to any issue, please make the corresponding changes in the very first comments body by adding \'Fixes #issue-number\' or \'Resolves #issue-number\'. If your pull request isn\'t linked to any issue, ignore this comment\!\""
echo $body
json="'{\"body\": ${body} }'"
echo $json
statusCode=`curl -X POST -H 'Accept: application/vnd.github+json' -H 'Authorization: token ****' -H 'Content-Type: application/json' https://api.github.com/repos/Ismoh/NoitaMP/issues/79/comments -d $json`
echo "statusCode="$statusCode

回显$body:

$ echo $body
"\:warning\: \:exclamation\: Your pull request is not linked to any issue, please make the corresponding changes in the very first comments body by adding \'Fixes #issue-number\' or \'Resolves #issue-number\'. If your pull request isn\'t linked to any issue, ignore this comment\!"

回显$json

$ echo $json
'{"body": "\:warning\: \:exclamation\: Your pull request is not linked to any issue, please make the corresponding changes in the very first comments body by adding \'Fixes #issue-number\' or \'Resolves #issue-number\'. If your pull request isn\'t linked to any issue, ignore this comment\!" }'

卷曲

$ curl -X POST -H 'Accept: application/vnd.github+json' -H 'Authorization: token ****' -H 'Content-Type: application/json
' https://api.github.com/repos/Ismoh/NoitaMP/issues/79/comments -d $json
{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue-comment"
}
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: Your
curl: (6) Could not resolve host: pull
curl: (6) Could not resolve host: request
curl: (6) Could not resolve host: is
curl: (6) Could not resolve host: not
curl: (6) Could not resolve host: linked
curl: (6) Could not resolve host: to
curl: (6) Could not resolve host: any
curl: (6) Could not resolve host: issue,
curl: (6) Could not resolve host: please
curl: (6) Could not resolve host: make
curl: (6) Could not resolve host: the
curl: (6) Could not resolve host: corresponding
curl: (6) Could not resolve host: changes
curl: (6) Could not resolve host: in
curl: (6) Could not resolve host: the
curl: (6) Could not resolve host: very
curl: (6) Could not resolve host: first
curl: (6) Could not resolve host: comments
curl: (6) Could not resolve host: body
curl: (6) Could not resolve host: by
curl: (6) Could not resolve host: adding
curl: (6) Could not resolve host: \'Fixes
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: or
curl: (6) Could not resolve host: \'Resolves
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: If
curl: (6) Could not resolve host: your
curl: (6) Could not resolve host: pull
curl: (6) Could not resolve host: request
curl: (6) Could not resolve host: isn\'t
curl: (6) Could not resolve host: linked
curl: (6) Could not resolve host: to
curl: (6) Could not resolve host: any
curl: (6) Could not resolve host: issue,
curl: (6) Could not resolve host: ignore
curl: (6) Could not resolve host: this
curl: (6) Could not resolve host: comment\!"
curl: (3) unmatched close brace/bracket in URL position 1:
}'

GitHub API 文档显示json数据如下:

curl \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER/comments \
  -d '{"body":"Me too"}'

有人可以告诉我我做错了什么吗?

是否有更好的方法来转义 json 使用的字符串?我已经尝试过

jq -Rsa .
,但也没有成功。

json bash curl jq
1个回答
2
投票

以下是如何使用 jq 构建 JSON:

body="\"\:warning\: \:exclamation\: Your pull request is not linked to any issue, please make the corresponding changes in the very first comments body by adding \'Fixes #issue-number\' or \'Resolves #issue-number\'. If your pull request isn\'t linked to any issue, ignore this comment\!\""
json="$(jq -n --arg body "$body" '{ $body }')"
# or: json=$(printf '%s\n' "$body" | jq -R '{ body: . }')

我怀疑上面所有的转义实际上都是必要的——为什么要转义冒号、感叹号或单引号?但 API 可能需要引用这些字符;我不明白为什么,因为它消耗 JSON。最好的办法可能是在分配正文时去掉上面的所有反斜杠(除了

"
之前的反斜杠)。

以下是如何正确扩展包含空格的变量,例如将其用作curl命令的有效负载时(请注意变量周围的引号)。变量应该总是被引用,除非你110%知道你在做什么:

curl -X POST \
 -H 'Accept: application/vnd.github+json' \
 -H 'Authorization: token ****' \
 -H 'Content-Type: application/json' \
 https://api.github.com/repos/Ismoh/NoitaMP/issues/79/comments \
 -d "$json"

有用的资源:

© www.soinside.com 2019 - 2024. All rights reserved.