使用 shell 脚本从文件测试 rest web 服务时如何获取成功计数、失败计数和失败原因

How to get success count, failure count and failure reason when testing rest webservices from file using shell script

您好,我正在通过多个 if 条件使用 shell 脚本测试 Web 服务,使用 shell 脚本编码,我得到了成功计数、失败计数和失败原因

success=0
failure=0

if curl -s --head --request DELETE http://localhost/bimws/delete/deleteUser?email=pradeepkumarhe1989@gmail.com | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
echo "DeleteUser is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

if curl -s --head  --request GET http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
 curl -s --head  --request GET http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com > f1.txt
 echo "getUserDetails is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

if curl -s -i -X POST -H "Content-Type:application/json"  http://localhost/bimws/post/addProjectLocationAddress -d '{"companyid":"10","projectid":"200","addresstypeid":"5","address":"1234 main st","city":"san jose","state":"CA","zip":"989898","country":"United States"}' | grep "200 OK" > /dev/null; then 
  success=$((success+1))
else
echo "addProjectLocationAddress is not working"$'\r' >> serverLog.txt
  failure=$((failure+1))
fi

echo $success Success
echo $failure failure

但我期待从一个文件中测试 Web 服务,就像我有一个名为 web_services.txt 的文件,其中包含我使用 shell 脚本的所有 Web 服务 我如何执行和成功计数,失败计数和失败原因

web_services.txt

都是不同的调用 delete、get 和 post

http://localhost/bimws/delete/deleteUser?email=pradeepkumarhe1989@gmail.com


http://localhost/bimws/get/getUserDetails?email=anusha4saju@gmail.com



http://localhost/bimws/post/addProjectLocationAddress -d '{"companyid":"10","projectid":"200","addresstypeid":"5","address":"1234 main st"
,"city":"san jose","state":"CA","zip":"989898","country":"United States"}'

首先,您当前的代码没有正确处理空行。你需要跳过那些。

您的行已包含 shell 命令。 运行 卷曲它们毫无意义。相反,您应该评估这些命令。

然后,需要修改curl,使其报告请求是否成功,添加-f:

FILE=D:/WS.txt
success=0
failure=0
while read LINE; do
    if test -z "$LINE"; then
        continue
    fi
    if eval $(echo "$LINE" | sed 's/^curl/curl -f -s/') > /dev/null; then 
        success=$((success+1))
    else
        echo $LINE >> aNewFile.txt
        failure=$((failure+1))
    fi
done < $FILE
echo $success Success
echo $failure failure