如何使用Python将一个值从一个API传递到另一个API?

rekjcdws  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(148)

我有两个API,API A和API B
我想从API A中读取imageID值,并将该值放在Api B中的“identifier”下。Api-A中有多个imageID,我需要循环并生成一个json。每次Api-A中出现新的imageID时,我如何替换identifier值?
API A

client_secret = ""

request = requests.Requestor()

path = "path to api”

 params = {
        "businessIdType": "ex",
        "businessId": "example",
        "limit": 50,
        Category": "abc"
    }


    params_str = "&".join([f"{k}={v}" for k, v in params.items()])
    url = "?".join([path, params_str])

    vulns = request.post(
        url=url,
        version=1,
        json = params
    )
    response_data = vulns.json()

    return response_data

         …

API A Json输出

"limit": 40,
"vulnerabilities": [
   {
      "labels": [
         {
            "labelKey": "abc",
            "labelValue": "def"
         }
      ],
      "imageId": "abcdefghi",
      "contId": "defghi"
      ...

API B

client_id = ""
client_secret = ""

request = requests.Requestor()

path = "path to api”

params = {
    "identifierType": "ImageId",

    "identifiers": ['abcdefghi']. ## <--- HOW DO I INSERT THE IMAGEID VALUE HERE FROM THE OUPUT FROM API A
}

    params_str = "&".join([f"{k}={v}" for k, v in params.items()])
    url = "?".join([path, params_str])
    # print(url)

    vulns_y = request.post(
        url=url,
        version=1,
        json = params
    )
    response_data_y = vulns_y.json()

    return response_data_y
mum43rcc

mum43rcc1#

要理解第一个代码块中的返回值在代码中发生了什么有点困难,但是只要您有来自API A的response_data,就可以获得它的imageID,如下所示:
response_data['vulnerabilities']['imageId']
因此,在API B中,将
"identifiers": ['abcdefghi']
与:
"identifiers": [response_data['vulnerabilities']['imageId']]
有关如何使用requests库https://www.geeksforgeeks.org/get-post-requests-using-python/的详细信息,请参见以下链接

相关问题