我编写了一个烟雾测试脚本,它使用 BASH 脚本和 Curl 来测试我们正在开发的 RESTful Web 服务。该脚本读取一个文件,并将每一行解释为 URL 后缀和 Curl REST 调用的参数。
不幸的是,当我调整它以运行 HTTP POST 调用和 GET 调用时,该脚本给出了意想不到的结果。与在脚本中运行命令相比,它不会给出相同的结果:
BASH 脚本:
IFS=$'\n' #Don't split an input URL line at spaces
RESTHOST='hostNameAndPath' #Can't give this out
URL="/activation/v2/activationInfo --header 'Content-Type:Application/xml'"
URL2="/activation/v2/activationInfo"
OUTPUT=`curl -sL -m 30 -w "%{http_code}" -o /dev/null $RESTHOST$URL -d @"./activation_post.txt" -X POST`
echo 'out:' $OUTPUT
OUTPUT2=`curl -sL -m 30 -w "%{http_code}" -o /dev/null $RESTHOST$URL2 --header 'Content-Type:Application/xml' -d @'./activation_post.txt' -X POST`
echo 'out2:' $OUTPUT2
结果输出:
输出:505
输出 2:200
因此,第一次调用失败(HTTP 返回码 505,不支持 HTTP 版本),第二次调用成功(返回码“OK”)。
为什么第一次通话失败,我该如何解决? 我已经验证他们应该执行相同的命令(在 echo 中评估)。我确信我缺少一些基本的东西,因为我现在正在学习 Bash 脚本。