Raghav Chadha
Raghav Chadha

Reputation: 127

Passing a dictionary key value in payload for a python request API

I am trying to create a script for an API request for a vmware application. I need to pass a variable from a dictionary in payload .

Here is the code which was able to develop from postman and output is taken as "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\" : \"[email protected]\" \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

However I need I want to pass the highlighted vcenter_name as a dictionary value something like this:-

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\" : \"[email protected]\" \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

Attaching screen shots for reference.

After adding inputs[‘vcenterkey’] in the payload , instead of the value in vcenter_name i am getting the exact `inputs[‘vcenterkey’] displayed in the payload which causes the API to fail in the script.

Upvotes: 1

Views: 6124

Answers (2)

Simon Black
Simon Black

Reputation: 923

It is best to use json library to formulate the payload and json.dumps() to create the string

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": "[email protected]"
      },
      {"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')

Upvotes: 4

Amjad Al Taleb
Amjad Al Taleb

Reputation: 151

This is not the correct way for handling json data. Use json library instead. Or you can try using formatted strings instead.

example:

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" : "[email protected]" },
                                     {"name" : "PASSWORD","value" : "Hidden-Password " } ],"others" : [ ],
                                    "otherAttributes" : { }},
                                    "monitoringInterval" : 1,
                                    "others" : [ ],
                                    "otherAttributes" : { }}

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

Upvotes: 0

Related Questions