【问题标题】:Bash Linux Curl send headers from variableBash Linux Curl 从变量发送标头
【发布时间】:2014-10-26 19:54:39
【问题描述】:

我在 bash 中有一些脚本。 我想向服务器发送一个带有一些标头的请求(cloudflare 块:)。

这是我写的:

headers="\
-H 'Host: somesite.com' \
-H 'Accept-Language: pl,en-US;q=0.7,en;q=0.3' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'DNT: 1' \
-H 'Connection: keep-alive'";

curl $headers somesite.com

但在输出中我有这个..

curl: (6) Couldn't resolve host 'Accept-Language'
curl: (6) Couldn't resolve host 'pl,en-US;q=0.7,en;q=0.3'
curl: (6) Couldn't resolve host 'Accept'
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'User-Agent'
curl: (6) Couldn't resolve host 'Mozilla'
curl: (6) Couldn't resolve host '(Windows'
curl: (6) Couldn't resolve host 'NT'
curl: (6) Couldn't resolve host '6.1;'

如何将变量中的标头放入命令中?

【问题讨论】:

  • 尝试引用变量curl "$headers" somesite.com
  • 要添加到@TomFenech,最佳做法是在变量包含空格时引用它。

标签: linux bash curl header


【解决方案1】:

不要为此使用字符串。试图在这样的字符串中正确引用基本上是不可能的,请使用数组。

请参阅http://mywiki.wooledge.org/BashFAQ/050 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 2014-09-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2018-02-16
    • 2012-11-26
    • 2012-01-13
    相关资源
    最近更新 更多