[英]Check json field from the curl response in linux
我正在 gitlab-ci 文件中编写脚本,我使用的服务器是 Linux 来运行管道。
现在我想做两个 API 调用,第二个调用依赖于第一个
例如
URL= you can take any HTTPS endpoint (i guess it really doesn't matter)
jsonResponse=$(curl -d $requestJson -X POST $FIRST_URL)
echo $jsonResponse
[{"result":"Success"}]
根据结果字段(成功/失败),我想使用 curl 进行另一个 API 调用。 像下面的东西
if response[0].result=success then "curl -d $requestJson -X POST $SECOND_URL" else exit 1
请注意,响应将在数组中。
您可能会做的是,您将从第一次 API 调用中保存 output 到文件 (api1-output.json),然后将其传递给下一个通讯员作业,您可以从中读取 Z78E6221F6393D1356DZF6393D1356DZF681DB 并根据其响应。
.gitlab-ci.yml文件中的示例:
api1_execution:
image: your_image
script:
- curl -XGET https://my-api1-endpoint.com/api/getSomething > scripts/api1-output.json
artifacts:
paths:
- scripts/api1-output.json
api2_execution:
image: your_image
script:
- API1_OUTPUT= $(cat scripts/api1-output.json)
- // do whatherever you want with API1_OUTPUT
- // fire 2nd API call
对于解析和读取 JSON output 我强烈建议您使用名为jQ的工具
上面的例子可以作为一个想法,从我的角度来看它是如何完成的。
我想做两个 rest API 调用和第二个 rest 调用取决于第一个响应
You could use some existing JSON library (eg jansson in C, or jsoncpp in C++) and combine them with some HTTP client library (eg libcurl ) and/or HTTP server library (eg libonion ).
您可能会对现有的 C++ 框架感兴趣,例如Qt或POCO 。
您应该查看最近的语言实现,例如Go或Rust或Ocaml或SBCL或Node.JS 。 它们提供了有用的库(与客户端和服务器端的 HTTP 和 REST 相关)。
You might consider using scripting languages like Python or Guile or Lua or Perl or Ruby , etc..... They also have relevant libraries for your needs.
我倾向于相信GNU bash不是满足您需求的最佳选择(至少也要考虑 GNU gawk )。
另请查看github和gitlab以了解与您的需求相关的开源实现。 一旦在纸上指定了这些(可能对要实现的消息使用EBNF表示法;另请阅读SOAP和JSONRPC ),实现相当容易。
请务必阅读Advanced Linux Programming and syscalls(2) and sockets(7) 。
验证该字段的正确方法是什么
另外,写几个有效字段和无效字段的例子。 然后考虑使用regex(7) 。 还可以研究与您的需求相似的现有开源软件的源代码以获得灵感。
另请参阅此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.