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?

More than 1 year has passed since last update.

curlコマンドの-d のjson内で環境変数を展開するためのクオート

Last updated at Posted at 2022-10-13

前提1

> echo '$AUTH_KEY'
$AUTH_KEY

> echo '"$AUTH_KEY"'
"$AUTH_KEY"

> echo "$AUTH_KEY"
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

> echo "'$AUTH_KEY'"
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

前提2

:pencil: jsonのキーバリューはダブルクオートで囲む必要がある。

よって

// NG ペイロードは {"name": "$NAME"} になる
$ curl -XPOST -H "Authorization: $AUTH_KEY" -H "Content-Type: application/json"\
  -d '{"name": "$NAME"}' https://localhost:3000/test

// OK
$ curl -XPOST -H "Authorization: $AUTH_KEY" -H "Content-Type: application/json"\
  -d "{\"name\": \"$NAME\"}" https://localhost:3000/test

とにかくダブルクオートのみ(必要に応じてエスケープ)を使え。と覚えておく。

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?