0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2025-02-20

検証

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

(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

(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

(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

(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

おまけ

$ BODY='fff'ggg'jjj'
$ echo $BODY
fffgggjjj
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?