通过curl发送json数据报错syntax error, expect {, actual [, pos 0

在shell里面通过curl发送json数据到另外一个服务器的时候报错syntax error, expect {, actual [, pos 0,死活都无法通过,代码片段为:

postcontent=`sed ":a;N;s/\n//g;ta" $OUTFILE`
echo $postcontent
curl -l -H "Content-type: application/json" -X POST -d '$postcontent' http://vpn.ciika.com/vpnport/updatestatus

其中postcontent为:

{"id":"sg1","in":891,"out":1753,"list":[{"server":"172.26.2.174:22","rate":3011},{"server":"clients","rate":1782}]}

开始有两个猜测,json是否是否被切断了,从打印的结果来看,是完整的,另外猜测-d里面的单引号是否有问题,经过尝试后发现确实是单引号的问题,修改为双引号问题解决:

curl -l -H "Content-type: application/json" -X POST -d "$postcontent" http://vpn.ciika.com/vpnport/updatestatus

参考:
https://stackoverflow.com/questions/26614423/trouble-with-bash-shell-script-attempting-to-post-variable-json-data-using-curl

标签: none

添加新评论