如何在.sh文件中调用 restapi



我正在尝试创建一个脚本来调用 restapi 端点,但我收到"错误的替换"错误。

这是我的脚本:

vi script.sh
echo "welcome to the script"
echo ${"http://web-integ000/sampleProject/getProductList"}

但是当我在命令行执行 curl 命令时,它起作用了。我使用了以下命令

$ curl -H "Accept:application/json" http://web-integ000/sampleProject/getProductList

我得到了以下输出:

[ {
  "productId" : "u1604028-5948abd0-0",
  "prodcutName":"H1ACX",
  "calendarDate" : "2017-06-20"
  }
 ]timeStamp:1497968810

如何使用脚本从 restapi 获取响应?

您可以使用简单的 curl 命令从服务器获取响应,如下所示:

rest.sh

result=$(curl -X GET --header "Accept: */*" "http://localhost:9090/employees")
echo "Response from server"
echo $result
exit

希望它有效!谢谢!

$ 计算 shell 命令或环境变量。操作系统无法原生评估 URL,因此您应该为其使用 curl 命令,例如:

VAR1=${curl ....}

然后,您可以使用该变量,例如回显它。

最新更新