在烧瓶中发送邮件请求

时间:2017-04-25 08:54:26

标签: python json firebase firebase-cloud-messaging

我正试图在烧瓶中发送邮件请求。

我想将Content-Type: application/json设置为标题的json对象发送。

我正在使用 requests 模块执行此操作,如下所示:

json_fcm_data = {"data":[{'key':app.config['FCM_APP_TOKEN']}], "notification":[{'title':'Wyslalem cos z serwera', 'body':'Me'}], "to":User.query.filter_by(id=2).first().fcm_token}
json_string = json.dumps(json_fcm_data)
print json_string
res = requests.post('https://fcm.googleapis.com/fcm/send', json=json_string)

但是这给了我:

  

TypeError:request()得到了一个意外的关键字参数'json'

有关如何解决此问题的任何建议吗?

1 个答案:

答案 0 :(得分:6)

首先修正错误:

你需要改变这个:

res = requests.post('https://fcm.googleapis.com/fcm/send', json=json_string)

到此:

res = requests.post('https://fcm.googleapis.com/fcm/send', data=json_string)

您获得的错误指出requests.post 不能接受名为json的参数,但它接受名为data的关键字参数,该参数可以在json格式。

然后添加标题:

如果要使用requests模块发送自定义标头,可以按以下方式执行:

headers = {'your_header_title': 'your_header'}
# In you case: headers = {'content-type': 'application/json'}
r = requests.post("your_url", headers=headers, data=your_data)

总结一切:

您需要稍微修复一下json格式。完整的解决方案将是:

json_data = {
    "data":{
        'key': app.config['FCM_APP_TOKEN']
    }, 
    "notification":{
        'title': 'Wyslalem cos z serwera', 
        'body': 'Me'
    }, 
    "to": User.query.filter_by(id=2).first().fcm_token
}

headers = {'content-type': 'application/json'}
r = requests.post(
    'https://fcm.googleapis.com/fcm/send', headers=headers, data=json.dumps(json_data)
)
相关问题