无法在curl中插入带有两个标头的变量

时间:2016-07-21 12:39:34

标签: bash curl

我有以下sh脚本

* Rebuilt URL to: ololo1"/
* Hostname was NOT found in DNS cache
* Could not resolve host: ololo1"
* Closing connection 0
curl: (6) Could not resolve host: ololo1"
* Rebuilt URL to: ololo2"/
* Hostname was NOT found in DNS cache
* Could not resolve host: ololo2"
* Closing connection 1
curl: (6) Could not resolve host: ololo2"
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#2)
> PUT / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8000
> Accept: */*
> Content-Type: application/json
> custom_ololo: value
> Content-Length: 40
> 
* upload completely sent off: 40 out of 40 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 400
< Date: Thu, 21 Jul 2016 12:32:13 GMT
< Server: WSGIServer/0.1 Python/2.7.6
< X-Frame-Options: SAMEORIGIN
< Content-Type: application/json
< 
* Closing connection 2

执行时记录:

-H "custom_ololo:  $value_for_header"

我们可以看到> custom_ololo: value效果良好function getBaseName($array){ $return=array(); foreach($array as $arr) { $exp=explode("/",$arr['url']); $return[]=array_pop($exp); } return $return; } $basenames[]=getBaseName($teams);

但字符串$ headers未正确插入。我已经尝试过把#&#34; $ header&#34;和$ {headers}但没有结果

所以,我的问题是:如何正确地将带有多个标题的字符串插入到带有curl的sh脚本中。

2 个答案:

答案 0 :(得分:2)

您需要使用一个阵列,此时您可以将所有标头放入阵列中并简化您的通话。

#!/usr/bin/env bash
value_for_header="value"
headers=(
  -H "custom1: ololo1"
  -H "custom2: ololo2"
  -H "Content-Type: application/json"
  -H "custom_ololo: $value_for_header"
)
curl -X "PUT" "${headers[@]}" http://localhost:8000/ -d '{"a": true}' -vv

答案 1 :(得分:1)

您需要将$标头放入&#34;&#34;

curl -X "PUT" -H "Content-Type: application/json" -H "custom_ololo:  $value_for_header" "$headers" http://localhost:8000/ -d '{"a": true}' -vv