【发布时间】:2017-08-16 20:17:07
【问题描述】:
我正在尝试使用 heredoc 传递我需要通过 curl 发出 POST 请求的 ONE 参数,这是我遇到的错误
./scripts/etcd.sh: line 10: warning: here-document at line 10 delimited by end-of-file (wanted `EOF{peerURLs:[http://etcd-$ordinal:2380]}EOF')
./scripts/etcd.sh: line 9: warning: here-document at line 9 delimited by end-of-file (wanted `EOF{peerURLs:[http://etcd-$ordinal:2380]}EOF')
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 42 100 42 0 0 12639 0 --:--:-- --:--:-- --:--:-- 21000
{"message":"unexpected end of JSON input"}
以及脚本中触发它的两行
request_body=$(cat <<EOF{\"peerURLs\":[\"http://etcd-$ordinal:2380\"]}EOF);
curl http://etcd-0.etcd:2379/v2/members -XPOST -H \"Content-Type: application/json\" --data \"$request_body\";
在问我自己的问题之前,我已经在这里Using curl POST with variables defined in bash script functions 尝试了所有答案。
编辑:
从下面我尝试过的答案和 cmets 中
curl http://etcd-0.etcd:2379/v2/members -XPOST -H \"Content-Type: application/json\" --data @<(cat <<EOF\n{\"peerURLs\":[\"http://etcd-$ordinal:2380\"]}\nEOF)
它可以工作,但给出了类似的错误
./scripts/etcd.sh: line 12: warning: here-document at line 10 delimited by end-of-file (wanted `EOF')
./scripts/etcd.sh: line 12: warning: here-document at line 10 delimited by end-of-file (wanted `EOF')
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
{"id":"a473da4d8f77b2b0","name":"","peerURLs":["http://etcd-3.etcd:2380"],"clientURLs":[]}
【问题讨论】:
-
Here-docs 不是这样工作的。它的内容必须从新行开始,结束标记必须单独在一行。您的脚本认为 整个事情 (
EOF{\"peerURLs\":[\"http://etcd-$ordinal:2380\"]}EOF);) 是您的标记。 -
您也不想转义引号。
curl -H "Content-Type..." --data "$request_body". -
考虑使用像
jq这样的工具来生成您的 JSON,尤其是当您嵌入参数的内容时。