【问题标题】:How to embed a string within a string in python2.7, that has escaped quotes in it?如何在python2.7中的字符串中嵌入一个字符串,其中已经转义了引号?
【发布时间】:2020-08-16 15:46:44
【问题描述】:

我正在尝试将两个文字替换为用于短信的有效负载的变量:

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 或文字

任何帮助将不胜感激

【问题讨论】:

标签: python string formatting sms


【解决方案1】:

您可以使用来自.post()json 属性,它将处理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})

【讨论】:

  • 对不起,我得到了错误:需要目的地参数,谢谢
  • @AWR 查看编辑,您可以使用json 参数从post 删除内容类型和json.dumps
  • 是的。再次感谢:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多