Python POST请求到Firebase的位置0处的意外标记END OF FILE

时间:2017-04-16 17:38:34

标签: python json firebase firebase-cloud-messaging

我正在尝试通过Firebase向某个客户端发送消息。这是我目前的(测试)代码:

Unit

产生以下错误,由Firebase返回:

import json
import requests
import urllib

def send_message():
    server = "https://fcm.googleapis.com/fcm/send"
    api_key = "xxx"
    user_token = "xxx"

    headers = {'Content-Type': 'application/json', 'Authorization': 'key=' + api_key}

    data = {"type": "dataUpdate"}
    payload = {"data": data, "to": user_token}
    payload = json.dumps(payload)

    res = requests.post(server, headers=headers, json=payload)

    return res

发送给Firebase的以下JSON对我来说似乎是正确的:

JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.

采用Firebase documentation描述的格式。知道为什么Firebase不接受给定的数据吗?

1 个答案:

答案 0 :(得分:4)

当您使用json=payload作为requests.post()的参数时,您无需在标头中指定'Content-Type': 'application/json'。另外,当参数应该是payload作为dict时,你传递一个字符串(即不需要json.dumps()

试试这个:

def send_message():
    server = "https://fcm.googleapis.com/fcm/send"
    api_key = "xxx"
    user_token = "xxx"

    headers = {'Authorization': 'key=' + api_key}

    data = {"type": "dataUpdate"}
    payload = {"data": data, "to": user_token}

    res = requests.post(server, headers=headers, json=payload)

    return res