提问者:小点点

在 /bin/sh脚本中使用curl访问Web服务会导致“HTTP 415不支持的媒体类型”


如果我运行以下命令,一切正常:

curl -s -o /dev/null -w %{http_code} \
http://www.example.com/mywebservice/generic.example.com \
--header "Content-type: application/json" --request PUT \
--data '{"hostname":"generic.example.com", "project":"none", "usage":"generic", "environment":"production"}'

当我将此命令放入 /bin/sh脚本中的变量中并执行它时,我在服务器日志中显示了“HTTP 415不支持的媒体类型”:

#!/bin/sh

read -d '' CMD << EOF
curl -s -o /dev/null -w %{http_code} http://www.example.com/mywebservice/generic.example.com \
--header "Content-type: application/json" --request PUT \
--data '{"hostname":"generic.example.com", "project":"none", "usage":"generic", "environment":"production"}'
EOF
`$CMD`

我通过打印验证了该命令,它看起来像我运行的手动命令:

#!/bin/sh

read -d '' CMD << EOF
curl -s -o /dev/null -w %{http_code} http://www.example.com/mywebservice/generic.example.com \
--header "Content-type: application/json" --request PUT \
--data '{"hostname":"generic.example.com", "project":"none", "usage":"generic", "environment":"production"}'
EOF
echo $CMD

输出:

curl-s-o/dev/null-w % { http _ code } http://www.example.com/mywebservice/generic.example.com-header " Content-type:application/JSON "-请求PUT-data ' { " hostname ":" generic . example . com "," project":"none "," usage":"generic "," environment":"production"} '


共1个答案

匿名用户

对我来说,它是这样工作的:

#!/bin/sh

read -d '' CMD << EOF
`/usr/bin/curl -s http://localhost:8080/someurl \
    --header "Content-type: application/json"`
EOF

echo $CMD

如果在EOF内部进行评估呢?