[英]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.