Python对Firebase的POST请求中位置0处出现意外的标记文件结尾

Python对Firebase的POST请求中位置0处出现意外的标记文件结尾,python,json,firebase,firebase-cloud-messaging,Python,Json,Firebase,Firebase Cloud Messaging,我正试图通过Firebase向某个客户发送消息。这是我当前的(测试)代码: 这将产生以下错误,由Firebase返回: JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0. 以下发送到Firebase的JSON对我来说似乎是正确的: { "data": { "type":"dataUpdate" }, "to":"xxx" } 其格式为所述的格式。知道Firebase为什么不接受给

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

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

JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.
以下发送到Firebase的JSON对我来说似乎是正确的:

{  
   "data": {  
      "type":"dataUpdate"
   },
   "to":"xxx"
}

其格式为所述的格式。知道Firebase为什么不接受给定的数据吗?

当您使用
json=payload
作为
请求的参数时。post()
您不需要在标题中指定
'Content-Type':'application/json'
。此外,当参数应为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

使用
json=payload
作为
requests.post()的参数时,不需要在标题中指定
'Content-Type':'application/json'
。此外,当参数应为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

@OpenUserX03为什么设置内容类型:application/json头会导致请求失败?我很担心,因为这样的事情可能会发生错误。@pubudodangoda不应该发生,如果指定
json
parameter@OpenUserX03为什么设置content-type:application/json头会导致请求失败?我很担心,因为这样的事情可能会发生错误。@pubudodangoda不应该这样做,如果指定
json
参数,就没有必要了