Python variable in json payload

Solved
damgol
Comes here often

Python variable in json payload

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"
}'''

1 Accepted Solution
PhilipDAth
Kind of a big deal
Kind of a big deal

Try changing the request to (and keep the prior payload format I supplied):

 

response = requests.request('POST', url, headers=headers, data = json.dumps(payload))

View solution in original post

5 Replies 5
PhilipDAth
Kind of a big deal
Kind of a big deal

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"}

 

 

PhilipDAth
Kind of a big deal
Kind of a big deal

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!

 

Get notified when there are additional replies to this discussion.