[英]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似乎是一个简单的问题,您应该在问这里之前自己尝试过
import requests
import json
response = requests.get('https://website.com/api/v2/data/apps/conf', headers={'accept': 'application/json', 'Api-Token': 'TOKEN'})
response = json.loads(response.text)
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.