Issue
This is what I am trying to get working:
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\!" }'
curl
$ 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 shows the json data as following:
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"}'
Is anyone out there, who can tell me what I am doing wrong?
Is there a better way to escape string for json usage? I already tried out jq -Rsa .
, but didn't work either.
Solution
Here's how to build that JSON with 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: . }')
I doubt that all that escaping above is actually necessary – why escape a colon, an exclamation mark or a single quote? But it might be that the API expects these character quoted; I don't see why though, since it consumes JSON. Best bet is probably to get rid of all the backslashes above when assigning body (except the ones before "
).
Here's how to correctly expand a variable which contains whitespace, for instance when using it as payload for a curl command (note the quotes around the variable). Variables should always be quoted, unless you know 110% what you are doing:
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"
Useful resources:
Answered By - knittl Answer Checked By - Mildred Charles (WPSolving Admin)