2016-07-21 12 views
2

füge ich folgende sh SkriptKann nicht variabel mit zwei Kopf in curl

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

Log wenn führen Sie es haben:

* 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 

Wie wir -H "custom_ololo: $value_for_header" funktioniert gut > custom_ololo: value

Aber sehen string $ headers wurde nicht korrekt eingefügt. Ich habe versucht, "$ Headers" und $ {headers} aber kein Ergebnis

Also, meine Frage ist: Wie wird richtig eingefügt Strings mit mehreren Headern in SH-Skript mit Curl.

Antwort

2

Sie müssen ein Array verwenden, an dem Sie alle die Header im Array setzen und Ihren Anruf vereinfachen können.

#!/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

Sie benötigen $ headers in ""

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

Ich habe versucht zu setzen. es funktioniert nicht. –

Verwandte Themen