python2.7-如何在python2.7的字符串中嵌入一个已转义引号的字符串?

问题描述

我正在尝试用变量替换两个文字获取短信的有效载荷:

import requests
url = "https://api.aql.com/v2/sms/send"
token='07ae4cca3f90a49347ccb5cfghypdngthwss85d9ce71862663e4b8162b366ba6c2db8f5f'
msg = "Ambient temperature has dropped to 10 Deg.C"
phone = "441234567890"

payload = "{\"destinations\" : [\"447753448024\"],\"message\": \"Hello,I am a message\"}"
headers = {
    'x-auth-token': token,'content-type': "application/json"
}

response = requests.post(url,data=payload,headers=headers)

print(response.text)

我希望在位置插入变量phone和msg或文字

任何帮助都会感激

解决方法

您可以使用json中的.post()属性,它将处理content-type标头以及从dictstr的转换

url = "https://api.aql.com/v2/sms/send"
token='07ae4cca3f90a49347ccb5cfghypdngthwss85d9ce71862663e4b8162b366ba6c2db8f5f'
msg = "Ambient temperature has dropped to 10 Deg.C"
phone = "441234567890"

payload = {'destinations':[phone],'message':msg}
response = requests.post(url,json=payload,headers={'x-auth-token': token})