ダブルクォーテーション、シングルクオーテーションが入った文字列を body に含めて curl を飛ばしたいとき

# 検証

GitHub API を使って、28番のPRに対してコメントを作成してみる。

## 準備
```
$ GITHUB_TOKEN="<YOUR GITHUB_TOKEN>"
$ ORG_NAME="<YOUR ORG_NAME>"
$ REPO_NAME="<YOUR REPO_NAME>"

$ PR_NUMBER="28"
```

## (1-1)
```
$ BODY="myBody"
$ echo $BODY
myBody
$ curl --fail-with-body -L -X POST \
            -H "Authorization: Bearer $GITHUB_TOKEN" \
            -H "Accept: application/vnd.github+json" \
            "https://api.github.com/repos/$ORG_NAME/$REPO_NAME/issues/$PR_NUMBER/comments" \
            -d "{\"body\": \"$BODY\"}"
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3508161/b157d5f3-e07c-4f8a-ab84-577bc164ef5c.png)

## (1-2)
エラーになってしまう。
```
$ BODY="myBody\"ccc\"ddd" # BODY='myBody"ccc"ddd' でも同じ意味
$ echo $BODY
myBody"ccc"ddd
$ curl --fail-with-body -L -X POST \
            -H "Authorization: Bearer $GITHUB_TOKEN" \
            -H "Accept: application/vnd.github+json" \
            "https://api.github.com/repos/$ORG_NAME/$REPO_NAME/issues/$PR_NUMBER/comments" \
            -d "{\"body\": \"$BODY\"}"
{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/issues/comments#create-an-issue-comment",
  "status": "400"
}
curl: (22) The requested URL returned error: 400
```
## (1-3)
```
$ BODY="body_my'ggg'www"
$ echo $BODY
body_my'ggg'www

$ curl --fail-with-body -L -X POST \
            -H "Authorization: Bearer $GITHUB_TOKEN" \
            -H "Accept: application/vnd.github+json" \
            "https://api.github.com/repos/$ORG_NAME/$REPO_NAME/issues/$PR_NUMBER/comments" \
            -d "{\"body\": \"$BODY\"}"
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3508161/a2dead8c-7421-4a49-bc69-acdae82b0017.png)


## (2-1)
```
$ BODY="my_body"
$ echo $BODY
my_body

$ jq -n \
  --arg body "$BODY" \
  '{body: $body}'
{
  "body": "my_body"
}

$ jq -n \
  --arg body "$BODY" \
  '{body: $body}' | \
  curl --fail-with-body -L -X POST \
    -H "Authorization: Bearer $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github+json" \
    "https://api.github.com/repos/$ORG_NAME/$REPO_NAME/issues/$PR_NUMBER/comments" \
    -d @-
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3508161/fdcc6c41-1d85-435b-918f-4d44eddd677f.png)


## (2-2)
```
$ BODY="my_body\"aaa\"bbb" # BODY='my_body"aaa"bbb' でも同じ意味
$ echo $BODY
my_body"aaa"bbb

$ jq -n \
  --arg body "$BODY" \
  '{body: $body}'
{
  "body": "my_body\"aaa\"bbb"
}

$ jq -n \
  --arg body "$BODY" \
  '{body: $body}' | \
  curl --fail-with-body -L -X POST \
    -H "Authorization: Bearer $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github+json" \
    "https://api.github.com/repos/$ORG_NAME/$REPO_NAME/issues/$PR_NUMBER/comments" \
    -d @-
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3508161/01ddb63c-2682-45bd-a0f0-9a6f47803718.png)

## (2-3)
```
$ BODY="body_my'eee'rrr"
$ echo $BODY
body_my'eee'rrr

$ jq -n \
  --arg body "$BODY" \
  '{body: $body}'
{
  "body": "body_my'eee'rrr"
}

$ jq -n \
  --arg body "$BODY" \
  '{body: $body}' | \
  curl --fail-with-body -L -X POST \
    -H "Authorization: Bearer $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github+json" \
    "https://api.github.com/repos/$ORG_NAME/$REPO_NAME/issues/$PR_NUMBER/comments" \
    -d @-
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3508161/c8540aac-87e5-4d34-b375-1c24987c152b.png)

# おまけ
```
$ BODY='fff'ggg'jjj'
$ echo $BODY
fffgggjjj
```