我正在使用 CURL 发送 JSON 数据。下面是一个例子:
mycomputer$ curl -H "Content-Type: application/json"
-d "{ "some_string": "Hello mom it's me!" }"
"http://localhost:3001/api_v2/blocks/42af6ab04d9d9635a97f8abec14ed023?api_key=fe5cf0d86af27c086ab5cd4d0eab6641"
Run Code Online (Sandbox Code Playgroud)
如何转义 的任何值的内容some_string
?
例如,如果有人想放入字符串Abe Lincoln's favorite character is the backslash \. He said "I love the \ and single quotes like ''".
,我如何在使用 curl 时转义它?
我想我需要做以下事情:
如果字符串包含一个"
带有三重反斜杠的转义符\\\"
。
如果字符串包含'
转义,则不需要 escaping '
。
\
带有三重反斜杠的转义符\\\\
。有没有我忘记的角色?
我假设您的目标只是让字符串通过 shell 的解析。如果是这样,请使用read
:
例如
$ IFS='' read -r var
Run Code Online (Sandbox Code Playgroud)
然后手动粘贴该线。
如果是多行,您可以使用:
$ IFS='' read -r -d '' var
Run Code Online (Sandbox Code Playgroud)
并再次粘贴,但这次使用CTRL+d结束输入。
或者使用定界符:
$ IFS='' read -r -d '' var <<'EOF'
{ "some_string": "Hello mom it's me!" }
EOF
Run Code Online (Sandbox Code Playgroud)
无论您使用哪种方法,您都可以使用该变量$var
来访问它:
$ curl -H "Content-Type: application/json" \
-d "$var" \
"http://localhost:3001/api_v2/blocks/42af6ab04d9d9635a97f8abec14ed023?api_key=fe5cf0d86af27c086ab5cd4d0eab6641"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28301 次 |
最近记录: |