dem*_*moo 1 python json firebase firebase-cloud-messaging
我正在尝试在烧瓶中发送发帖请求。
我想发送Content-Type: application/json设置为标头的json对象。
我正在使用请求模块执行以下操作:
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)
Run Code Online (Sandbox Code Playgroud)
但这给了我:
TypeError:request()得到了意外的关键字参数'json'
有关如何解决此问题的任何建议?
首先修复错误:
您需要更改此:
res = requests.post('https://fcm.googleapis.com/fcm/send', json=json_string)
Run Code Online (Sandbox Code Playgroud)
对此:
res = requests.post('https://fcm.googleapis.com/fcm/send', data=json_string)
Run Code Online (Sandbox Code Playgroud)
您得到的错误状态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)
Run Code Online (Sandbox Code Playgroud)
总结一下:
您需要修复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)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7450 次 |
| 最近记录: |