Hello,
I am not JSON expert so I am looking for help.
I want to pass on a python variable to payload in JSON format.
I was trying with code below but it doesn't work. I have info that JSON submitted is wrong.
My code is:
------------------------------------------------------------------------------------------------------------------------------------
import requests
url = "https://api.meraki.com/api/v1/organizations/XXX/configTemplates"
myname = "TESTNAME"
payload = '''{
"name": myname,
"timeZone": "America/Los_Angeles"
}'''
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"X-Cisco-Meraki-API-Key": ""
}
response = requests.request('POST', url, headers=headers, data = payload)
print(response.text.encode('utf8'))
------------------------------------------------------------------------------------------------------------------------------------
Of course payload below works perfectly but I need variables to be able to pass them through argv in the future.
payload = '''{
"name": "TESTNAME",
"timeZone": "America/Los_Angeles"
}'''
Solved! Go to solution.
Try changing the request to (and keep the prior payload format I supplied):
response = requests.request('POST', url, headers=headers, data = json.dumps(payload))
Try:
payload = {
"name": myname,
"timeZone": "America/Los_Angeles"
}
payload = {
"name": myname,
"timeZone": "America/Los_Angeles"
}
It doesn't work:
{"status":400,"error":"Bad request: There was a problem in the JSON you submitted"}
Try changing the request to (and keep the prior payload format I supplied):
response = requests.request('POST', url, headers=headers, data = json.dumps(payload))
You'll also need to add at the top:
import json
Yes. It works.
Thank you very much!