简体   繁体   English

从 linux 中的 curl 响应中检查 json 字段

[英]Check json field from the curl response in linux

I am writing a script in gitlab-ci file and the server I am using is Linux to run the pipeline.我正在 gitlab-ci 文件中编写脚本,我使用的服务器是 Linux 来运行管道。

Now I want to make two API call and 2nd call is dependent on the 1st现在我想做两个 API 调用,第二个调用依赖于第一个

for example例如

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"}]

Based on the result field (success/failure) I want to make another API call using curl.根据结果字段(成功/失败),我想使用 curl 进行另一个 API 调用。 something like below像下面的东西

if response[0].result=success then "curl -d $requestJson -X POST $SECOND_URL" else exit 1

Note that the response will be in Array.请注意,响应将在数组中。

What you could possibly do is that you will save the output from 1st API call to a file (api1-output.json) and then pass it to the next correspondent job from where you can read that output and act based on its response.您可能会做的是,您将从第一次 API 调用中保存 output 到文件 (api1-output.json),然后将其传递给下一个通讯员作业,您可以从中读取 Z78E6221F6393D1356DZF6393D1356DZF681DB 并根据其响应。

Example in your .gitlab-ci.yml file: .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

For parsing and reading JSON output I highly recommend that you use tool called jQ对于解析和读取 JSON output 我强烈建议您使用名为jQ的工具

The above example can serve to you just as an idea how it can be done from my perspective.上面的例子可以作为一个想法,从我的角度来看它是如何完成的。

I want to make two rest API call and 2nd rest call is dependent on the 1st response我想做两个 rest API 调用和第二个 rest 调用取决于第一个响应

Please reason in terms of protocols: so HTTPS (or HTTP ) and REST .请根据协议进行推理:所以HTTPS (或HTTP )和REST Read much more about both of them.阅读更多关于它们的信息。

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 ). 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 ).

You might be interested by existing C++ frameworks such as Qt or POCO .您可能会对现有的 C++ 框架感兴趣,例如QtPOCO

You should look at recent language implementations, such as Go or Rust or Ocaml or SBCL or Node.JS .您应该查看最近的语言实现,例如GoRustOcamlSBCLNode.JS They provide useful libraries (related to HTTP and REST, both client and server side).它们提供了有用的库(与客户端和服务器端的 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. 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.

Read the documentation of the programming languages implementations and libraries you want to use.阅读您要使用的编程语言实现和库的文档。

I tend to believe that GNU bash is not the best alternative for your needs (consider also at least GNU gawk ).我倾向于相信GNU bash不是满足您需求的最佳选择(至少也要考虑 GNU gawk )。

Look also on both github and gitlab for open source implementations relevant to your needs.另请查看githubgitlab以了解与您的需求相关的开源实现。 Once these are specified (perhaps usingEBNF notation for the messages you want to implement; read also about SOAP and JSONRPC ) on paper, the implementation is fairly easy.一旦在纸上指定了这些(可能对要实现的消息使用EBNF表示法;另请阅读SOAPJSONRPC ),实现相当容易。

Be sure to read Advanced Linux Programming and syscalls(2) and sockets(7) .请务必阅读Advanced Linux Programming and syscalls(2) and sockets(7)

What will correct way to validate the field验证该字段的正确方法是什么

First, specify on paper what are valid fields.首先,在纸上指定哪些是有效字段。

Also, write several examples of valid fields, and of invalid fields.另外,写几个有效字段和无效字段的例子。 Then consider using regex(7) .然后考虑使用regex(7) Also study for inspiration the source code of existing open source software similar to your needs .还可以研究与您的需求相似的现有开源软件的源代码以获得灵感。

See also this .另请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM