首页 > 解决方案 > bash, braces and quotes and curl -d

问题描述

I'm trying to send some headers and key value pairs of data to my server using curl. It looks like I'm getting a bash quoting problem, my key value pairs are being treated like hosts.

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 is telling me:

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.

and error.txt starts off like this:

* 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
...

I certainly don't mean to hit the hosts baz, glern, etc... and it certainly looks like a shell quoting problem. But after trying many different things, like adding extra \" near the existing bare " around my key value pairs, I'm a bit lost here.

标签: bashcurl

解决方案


在 之后-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"}

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


推荐阅读