0

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.

knittl
  • 246,190
  • 53
  • 318
  • 364
Ismoh
  • 1,074
  • 2
  • 12
  • 35
  • 2
    Double-quote your variable references, e.g. `"$json"` instead of just `$json`. See ["When should I double-quote a parameter expansion?"](https://stackoverflow.com/questions/55023461/when-should-i-double-quote-a-parameter-expansion) (short answer: almost always). – Gordon Davisson Oct 04 '22 at 00:42
  • 5
    Don't try to create JSON by hand with string operations. Use the `jq` utility. – Barmar Oct 04 '22 at 01:23
  • Thanks for the replies! I am going to try quoting! I already tried jq, but I still didnt understand it! – Ismoh Oct 04 '22 at 10:41
  • "$json" doesnt work. GitHubs response: Unable to parse JSON. – Ismoh Oct 04 '22 at 15:07
  • 1
    Shell scripting 101: `"$json"`, not `$json` to preserve whitespace in values. – knittl Oct 04 '22 at 19:35

1 Answers1

1

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:

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 1
    Thanks a ton! I escaped everything just because I am new to this and I was struggling. If you want me to do a favour, would it be possible to add a doc link to jq? Because I don't want to just copy paste stuff. I really want to learn! – Ismoh Oct 04 '22 at 20:35
  • 1
    @Ismoh added several useful links to the answer – knittl Oct 04 '22 at 20:39
  • Just for your info: line 3 in your example is working. line 2 isn't. – Ismoh Oct 05 '22 at 22:33
  • 1
    @Ismoh oh, thanks for the hint. Line 2 was missing `-n` (`--null-input`), because it doesn't read any JSON from stdin. – knittl Oct 06 '22 at 06:28