I'm calling an API that will require dynamic variables as parameters. However, I do not know how to format the string to include variables when it is surrounded by triple quotes and a backslash escape character at the beginning of the string literal.
I've tried varying the amount of quotes and using the ".format()" function.
Here is code formatted in a way that gets a successful result:
payload = "{\n\t\"firm\": \"myfirm\",\n\t\"id\": \"f87987562\",\n\t\"data\": {\n\t\t\"tracking_preference\": 2\n\t} \n}\n"
Here is my attempt at trying to format the string in a cleaner way while also including variables:
payload = \
"""{
"firm": {0},
"id": {1},
"data": {
"tracking_preference": {2}
}
}
""".format('myfirm', "f87987562", 2)
This is the error that I am receiving:
19 }
20 }
---> 21 """.format('myfirm', "f87987562", 2)
22
23 apikey = "secret_key"
KeyError: '\n "firm"'
I suspect it has something to do with the backslash, but its implementation seems necessary. Any help and insight on the intuition behind this string formatting is greatly appreciated.
I am trying to pass the string literal into the request function:
response = requests.request("POST", url, data=payload, headers=headers)