简体   繁体   English

REST API - 尝试从 JSON 中提取值并将其附加到 DELETE 操作

[英]REST API - trying to extract a value from JSON and appending it to a DELETE operation

I want to run a CURL command to query a REST API.我想运行 CURL 命令来查询 REST API。

After running the command:运行命令后:

curl -X 'GET' \
  'https://website.com/api/v2/data/apps/conf' \
  -H 'accept: application/json' \
  -H 'Api-Token: TOKEN'

I am getting the JSON response that looks like我收到的 JSON 响应看起来像

   "data" : {
      "conf" : [
         {
            "app_id" : 2,
            "app_name" : "Test App" : false,
            "host" : "10.10.10.10",
            "app_protocol" : "http",
            "protocols" : [
               {
                  "created_at" : "2022-06-06T11:25:40.444Z",
                  "id" : 103,
                  "port" : "22",
                  "service_id" : 2,
                  "transport" : "tcp",
                  "updated_at" : "2022-06-06T11:25:40.444Z"
               }
            ],
            "reachability" : null,
            "assignments" : [
               {
                  "primary" : null,
                  "id" : 5,
                  "reachability" : null,
                  "service_id" : 2
               }
            ],
            "trust_self_signed_certs" : true,
            "use_check" : false
         },
         {

I am now trying to extract all app_id values and need to run several CURL requests to我现在正在尝试提取所有 app_id 值并需要运行几个 CURL 请求以

curl -X 'DELETE' \
  'https://website.com/api/v2/data/apps/conf/**<APP_ID>**' \
  -H 'accept: application/json' \
  -H 'Api-Token: TOKEN'

in order to delete these individal app_ids.为了删除这些单独的 app_ids。

I hope you guys can quickly help me out here我希望你们能在这里快速帮助我

thanks a lot, appreciated非常感谢,不胜感激

Seems like a simple question you should have tried it by yourself before asking here似乎是一个简单的问题,您应该在问这里之前自己尝试过

  • First import requests wich will allow you to call your api and json to convert the returned values to python objects:首先导入请求将允许您调用您的 api 和 json 以将返回的值转换为 python 对象:
import requests
import json
  • Then do the first request and get the json:然后执行第一个请求并获取 json:
response = requests.get('https://website.com/api/v2/data/apps/conf', headers={'accept': 'application/json', 'Api-Token': 'TOKEN'})
response = json.loads(response.text)
  • You can then scrolle throught the json and send the request:然后,您可以滚动浏览 json 并发送请求:
     for conf in response['data']['conf']: app_id = conf['app_id'] requests.delete(f'https://website.com/api/v2/data/apps/conf/{app_id}', headers={'accept': 'application/json', 'Api-Token': 'TOKEN'})

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

相关问题 尝试使用 JSON 从 API 返回一个值 - Trying to return a value from API with JSON Python:尝试提取某些键时,如何避免在某些 dict 元素中出现 KeyError,APi json 中缺少键值? - Python: when trying to extract certain keys, how can I avoid a KeyError when in some dict elements, the key value is missing from APi json? Marklogic REST API-从文档中提取数据 - Marklogic REST API - Extract data from documents 将 API 中的多个 JSON 附加到单个 JSON 文件中 - Appending multiple JSON from API into singular JSON file 从json字符串中提取值 - Extract value from json string Python从Json提取值 - Python Extract Value from Json 如何从 Json 响应中提取特定值并在 Rest Assured Automation Testing 的输入有效负载 json 文件中使用此值 - How to extract a specific value from Json response and use this value in the input payload json file in Rest Assured Automation Testing 如何从请求中提取“响应”的值,然后在后续请求中将其用作RESTUI的SOAPUI和POSTMAN中的标头 - How can I extract the value of the 'response' from a request and then use it in subsequent requests as Header in SOAPUI and POSTMAN for rest api 如何从API中提取价值 - How to extract value from API 使用来自网页/网络服务器的休息请求提取值 - Extract value with rest request from webpage / webserver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM