Ausgabe
Das versuche ich zum Laufen zu bringen:
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:
$ 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\!"
echo $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\!" }'
kräuseln
$ 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 zeigt die JSON-Daten wie folgt:
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"}'
Ist jemand da draußen, der mir sagen kann, was ich falsch mache?
Gibt es eine bessere Möglichkeit, Zeichenfolgen für die Verwendung von JSON zu entkommen? Habe ich schon ausprobiert, jq -Rsa .
hat aber auch nicht funktioniert.
Lösung
So erstellen Sie diesen JSON mit jq:
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: . }')
Ich bezweifle, dass all das obige Escapezeichen wirklich notwendig ist – warum einen Doppelpunkt, ein Ausrufezeichen oder ein einfaches Anführungszeichen maskieren? Aber es kann sein, dass die API diese Zeichen in Anführungszeichen erwartet; Ich verstehe jedoch nicht warum, da es JSON verbraucht. Am besten ist es wahrscheinlich, alle Backslashes oben loszuwerden, wenn Sie body zuweisen (außer denen vor "
).
So erweitern Sie eine Variable, die Leerzeichen enthält, beispielsweise wenn Sie sie als Nutzlast für einen Curl-Befehl verwenden (beachten Sie die Anführungszeichen um die Variable herum). Variablen sollten immer in Anführungszeichen gesetzt werden, es sei denn, Sie wissen zu 110 %, was Sie tun:
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"
Nützliche Ressourcen:
- jq 1.6 Handbuch
- jq-Wiki
- jq-FAQ
- jq kochbuch
- Fortgeschrittene Themen
- Eine Stream-orientierte Einführung in jq
Beantwortet von – knittl
Antwort geprüft von – Jay B. (FixError Admin)