【问题标题】:Passing a dictionary key value in payload for a python request API在有效负载中为 python 请求 API 传递字典键值
【发布时间】:2020-09-04 19:53:10
【问题描述】:

我正在尝试为 vmware 应用程序的 API 请求创建脚本。我需要从有效载荷的字典中传递一个变量。

这是能够从邮递员开发的代码,输出被视为“python请求”:-

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

但是我需要将突出显示的 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')

)

附上屏幕截图以供参考。

在负载中添加输入['vcenterkey'] 后,我得到的不是 vcenter_name 中的值,而是在负载中显示的确切的 `inputs['vcenterkey'],这会导致 API 在脚本中失败。

【问题讨论】:

    标签: python json api python-requests postman


    【解决方案1】:

    最好使用json库来制定payload和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')
    

    【讨论】:

      【解决方案2】:

      这不是处理 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)
      

      【讨论】:

      • 你能帮我看看我应该在哪里编辑这个。我该怎么办。概述会很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2019-08-13
      • 2020-04-28
      • 2021-03-12
      • 2020-04-30
      • 2018-10-13
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多