【问题标题】:Putting a string into a curl command without escape chars将字符串放入不带转义字符的 curl 命令中
【发布时间】:2015-11-30 18:03:26
【问题描述】:

好的,这可能非常明显,但对于我的生活,我无法弄清楚。 我想从带有 -d 标签的脚本中调用 curl 命令来传递一些 JSON

#!/bin/bash
JSON_STR='{"Some":"JSON","Data":"Here"}'
curl -x PUT "http://localhost:port/api/url" -d $JSON_STR

现在如果我回显 curl 命令来检查它的样子

echo curl -x PUT "http://localhost:port/api/url" -d $JSON_STR

一切看起来都很好:

curl -x PUT "http://localhost:port/api/url" -d '{"Some":"JSON","Data":"Here"}'

但是 API 抱怨转义字符,它看到

'{\"Some\":\"JSON\",\"Data\":\"Here\"}'

现在我明白了它们为什么存在(转义字符),但是如何在脚本命令中摆脱它们呢? bash 中有原始字符串吗?

提前致谢

【问题讨论】:

  • curl 的手册页对于 -d 选项说数据应该是 "url-encoded"。你试过这样做吗?

标签: linux string bash scripting


【解决方案1】:

通过不使用 JSON_STR 并仅使用以下内容来解决此问题

curl -x PUT "http://localhost:port/api/url" \
    -d '{"Some":"JSON","Data":"'"$VARIABLE"'"}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多