這就是我想要開始的作業:
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
回聲$體:
$ 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 Docs顯示 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 .,但也沒有用。
uj5u.com熱心網友回復:
以下是使用 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。最好的選擇可能是在分配 body 時去掉上面的所有反斜杠(除了之前的那些")。
以下是如何正確擴展包含空格的變數,例如將其用作 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"
有用的資源:
- jq 1.6 手冊
- jq維基
- jq 常見問題
- jq食譜
- 高級主題
- 面向流的 jq 介紹
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512913.html
標籤:json重击卷曲jq
