在Python请求API中传递字典键值作为负载

0 投票
2 回答
5272 浏览
提问于 2025-06-18 04:07

我正在为一个vmware应用程序创建一个API请求的脚本。我需要从一个字典中传递一个变量到请求的内容里。

这是我从Postman中开发出来的代码,输出格式是“python requests”:-

import requests
url = "url@domain/api-path"
payload = "{\r\n  \"name\" : \"VC Adapter Instance\",\r\n  \"description\" : \"A vCenter Adapter Instance\",\r\n  \"collectorId\" : \"1\",\r\n  \"adapterKindKey\" : \"VMWARE\",\r\n  \"resourceIdentifiers\" : [ {\r\n    \"name\" : \"AUTODISCOVERY\",\r\n    \"value\" : \"true\"\r\n  }, {\r\n    \"name\" : \"PROCESSCHANGEEVENTS\",\r\n    \"value\" : \"true\"\r\n  }, {\r\n    \"name\" : \"VCURL\",\r\n    \"value\" : \"vcenter_name\" \r\n  } ],\r\n  \"credential\" : {\r\n    \"id\" : null,\r\n    \"name\" : \"Added Credential\",  \r\n    \"adapterKindKey\" : \"VMWARE\",\r\n    \"credentialKindKey\" : \"PRINCIPALCREDENTIAL\",\r\n    \"fields\" : [ {\r\n      \"name\" : \"USER\",\r\n      \"value\" : \"administrator@vsphere.local\" \r\n    }, {\r\n      \"name\" : \"PASSWORD\",\r\n      \"value\" : \"Hidden-Password \" \r\n    } ],\r\n    \"others\" : [ ],\r\n    \"otherAttributes\" : { }\r\n  },\r\n  \"monitoringInterval\" : 1,\r\n  \"others\" : [ ],\r\n  \"otherAttributes\" : { }\r\n}\r\n"
headers = {
  'Accept': 'application/json',
  'Authorization': 'Hidden Token',
  'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))

enter image description here

不过,我想把高亮的vcenter_name作为字典的一个值传递,类似这样:-

import requests
url = "url@domain/api-path"
Inputs = {‘vcenterkey’ : ‘vcenter_name’}

payload = "{\r\n  \"name\" : \"VC Adapter Instance\",\r\n  \"description\" : \"A vCenter Adapter Instance\",\r\n  \"collectorId\" : \"1\",\r\n  \"adapterKindKey\" : \"VMWARE\",\r\n  \"resourceIdentifiers\" : [ {\r\n    \"name\" : \"AUTODISCOVERY\",\r\n    \"value\" : \"true\"\r\n  }, {\r\n    \"name\" : \"PROCESSCHANGEEVENTS\",\r\n    \"value\" : \"true\"\r\n  }, {\r\n    \"name\" : \"VCURL\",\r\n    \"value\" : \"inputs[‘vcenterkey’] \" \r\n  } ],\r\n  \"credential\" : {\r\n    \"id\" : null,\r\n    \"name\" : \"Added Credential\",  \r\n    \"adapterKindKey\" : \"VMWARE\",\r\n    \"credentialKindKey\" : \"PRINCIPALCREDENTIAL\",\r\n    \"fields\" : [ {\r\n      \"name\" : \"USER\",\r\n      \"value\" : \"administrator@vsphere.local\" \r\n    }, {\r\n      \"name\" : \"PASSWORD\",\r\n      \"value\" : \"Hidden-Password \" \r\n    } ],\r\n    \"others\" : [ ],\r\n    \"otherAttributes\" : { }\r\n  },\r\n  \"monitoringInterval\" : 1,\r\n  \"others\" : [ ],\r\n  \"otherAttributes\" : { }\r\n}\r\n"
headers = {
  'Accept': 'application/json',
  'Authorization': 'Hidden Token',
  'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8')

)

enter image description here

附上截图供参考。

在payload中添加inputs[‘vcenterkey’]后,我并没有得到vcenter_name中的值,而是直接在payload中显示了`inputs[‘vcenterkey’]`,这导致API在脚本中失败。

相关问题:

  • 暂无相关问题
暂无标签

2 个回答

0

这不是处理json数据的正确方法。应该使用json库来处理。或者你也可以试试使用格式化字符串。

示例:

import json

inputs = {'vcenterkey' : 'vcenter_name'}

payload = {"name": "VC Adapter Instance",
            "description" : "A vCenter Adapter Instance",
            "collectorId" : "1",
            "adapterKindKey" : "VMWARE",
            "resourceIdentifiers" :
                [{"name" : "AUTODISCOVERY","value" : "true"},
                {"name" : "PROCESSCHANGEEVENTS","value" : "true"},
                {"name" : "VCURL","value" : inputs['vcenterkey']}],
            "credential": {"id" : "null",
                           "name" : "Added Credential",
                           "adapterKindKey" : "VMWARE",
                           "credentialKindKey" :"PRINCIPALCREDENTIAL",
                           "fields":[{"name" : "USER","value" : "administrator@vsphere.local" },
                                     {"name" : "PASSWORD","value" : "Hidden-Password " } ],"others" : [ ],
                                    "otherAttributes" : { }},
                                    "monitoringInterval" : 1,
                                    "others" : [ ],
                                    "otherAttributes" : { }}

payload = json.dumps(payload)
print(payload)
4

最好使用json库来构建数据内容,然后用json.dumps()来生成字符串。

import requests
import json

url = "url@domain/api-path"
inputs = {'vcenterkey': 'vcenter_name'}

payload = {
    "name": "VC Adapter Instance",
    "description" : "A vCenter Adapter Instance",
    "collectorId" : "1",
    "adapterKindKey" : "VMWARE",
    "resourceIdentifiers": [
      {"name": "AUTODISCOVERY",
      "value": "true"}, 
      {"name": "PROCESSCHANGEEVENTS",
       "value": "true"},
      {"name": "VCURL",
       "value": inputs['vcenterkey']
      }],
    "credential": {
      "id" : None,
      "name" : "Added Credential",
      "adapterKindKey": "VMWARE",
      "credentialKindKey": "PRINCIPALCREDENTIAL",
      "fields": [{
        "name": "USER",
        "value": "administrator@vsphere.local"
      },
      {"name": "PASSWORD",
      "value" : "Hidden-Password"}],
      "others" : [ ],
      "otherAttributes": { }},
      "monitoringInterval": 1,
      "others" : [ ],
      "otherAttributes": { }}

headers = {
  'Accept': 'application/json',
  'Authorization': 'Hidden Token',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, 
headers=headers, data = json.dumps(payload))
print(response.text.encode('utf8')

撰写回答