bash、大括号、引号和curl-d

bash、大括号、引号和curl-d,bash,curl,Bash,Curl,我正在尝试使用curl将一些头和键值对数据发送到我的服务器。看起来我遇到了一个bash引用问题,我的键值对被视为主机 curl -v --header "Content-Type: application/json" \ --header "Foo: Bar 123456" \ -d \"\{ "baz": "glern", "froboz": "foo again"\}\" \ https://example.com > foo.html 2> error.txt examp

我正在尝试使用curl将一些头和键值对数据发送到我的服务器。看起来我遇到了一个bash引用问题,我的键值对被视为主机

curl -v --header "Content-Type: application/json" \
--header "Foo: Bar 123456"  \
-d \"\{ "baz": "glern", "froboz": "foo again"\}\" \
https://example.com > foo.html 2> error.txt 
example.com告诉我:

HTTP 404 Not Found: URL or Document not found (dns_unresolved_hostname) 

HTTP 404: Your requested host "baz" could not be resolved by DNS. The document at the specified URL does not exist.

HTTP 404 Not Found: URL or Document not found (dns_unresolved_hostname) 

HTTP 404: Your requested host "glern," could not be resolved by DNS. The document at the specified URL does not exist.
error.txt的开头是这样的:

* Rebuilt URL to: baz:/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 10.196.133.236...
* TCP_NODELAY set
* Connected to http.proxy.mycompanyproxy.com (10.196.133.236) port 8000 (#0)
> POST http://baz:/ HTTP/1.1
> Host: baz
> User-Agent: curl/7.54.0
> Accept: */*
> Proxy-Connection: Keep-Alive
> Content-Type: application/json
> Foo: Bar 123456
> Content-Length: 2
> } [2 bytes data]
* upload completely sent off: 2 out of 2 bytes
< HTTP/1.1 404 Not Found
...
*重建URL至:baz:/
%总接收百分比%x平均速度时间电流
数据加载上载总左速度
0 0 0 0 0 0 0--:-:-:-:-:-:---:---0*正在尝试10.196.133.236。。。
*TCP_节点集
*已连接到http.proxy.mycompanyproxy.com(10.196.133.236)端口8000(#0)
>职位http://baz:/ HTTP/1.1
>主持人:巴兹
>用户代理:curl/7.54.0
>接受:*/*
>代理连接:保持活动
>内容类型:application/json
>富:酒吧123456
>内容长度:2
>}[2字节数据]
*完全发送的上载:2个字节中的2个
未找到

我当然不是故意要攻击主持人baz,glern等等。。。当然,这看起来像是一个外壳报价问题。但是在尝试了许多不同的事情之后,比如在我的键值对周围添加了额外的\“靠近现有的裸”之后,我在这里有点迷茫。

-d
之后,只有
\{
形成了
-d
的参数。现在还不完全清楚你在追求什么,但可能:

curl -v --header "Content-Type: application/json" \
     --header "Foo: Bar 123456"  \
     -d '{ "baz": "glern", "froboz": "foo again"}' \
     https://example.com > foo.html 2> error.txt 
这样,
-d
的参数是:

{ "baz": "glern", "froboz": "foo again"}

没有必要在包含大量双引号的字符串周围使用双引号;这只会让生活变得更艰难。

-d
之后,只有
\{
构成
-d
的参数。不清楚你在追求什么,但可能:

curl -v --header "Content-Type: application/json" \
     --header "Foo: Bar 123456"  \
     -d '{ "baz": "glern", "froboz": "foo again"}' \
     https://example.com > foo.html 2> error.txt 
这样,
-d
的参数是:

{ "baz": "glern", "froboz": "foo again"}

没有必要在包含大量双引号的字符串周围使用双引号;这只会让生活变得更艰难。

考虑一下,如果您只是有一个文件,您的数据会是什么样子:

$ cat tmp.json
{ "baz": "glern", "froboz": "foo again" }
在这种情况下,您只需使用

$ curl ... -d "$(cat tmp.json)"
现在只需“手动”执行命令替换(从双引号切换到单引号以避免在命令行上转义一堆引号):

不过,一般来说,最好让
jq
之类的工具为您生成JSON,而不是尝试手动编写;这可以在需要时大大简化引用

$ curl ... -d "$(jq -n '{baz: "glern", froboz: "foo again"}')"

如果你的JSON不是硬编码的,而是来自变量,比如
{“baz”:“$SOME_VAR”}
,那么使用这样一个工具几乎是强制性的。与其跳转来确保
SOME_VAR
中的所有内容都正确转义,不如让
jq
为你做:
jq-n--argjson x“$SOME_VAR”{baz:$x}“
。请注意,
$x
不是一个shell变量;它是一个
jq
变量。

考虑一下,如果您只是有一个文件,数据会是什么样子:

$ cat tmp.json
{ "baz": "glern", "froboz": "foo again" }
在这种情况下,您只需使用

$ curl ... -d "$(cat tmp.json)"
现在只需“手动”执行命令替换(从双引号切换到单引号以避免在命令行上转义一堆引号):

不过,一般来说,最好让
jq
之类的工具为您生成JSON,而不是尝试手动编写;这可以在需要时大大简化引用

$ curl ... -d "$(jq -n '{baz: "glern", froboz: "foo again"}')"
如果你的JSON不是硬编码的,而是来自变量,比如
{“baz”:“$SOME_VAR”}
,那么使用这样一个工具几乎是强制性的。与其跳转来确保
SOME_VAR
中的所有内容都正确转义,不如让
jq
为你做:
jq-n--argjson x“$SOME_VAR”{baz:$x}“
。请注意,
$x
不是shell变量;它是
jq
变量