向Firebase发出简单的POST请求。出于某种原因,它不起作用。具有相同数据的cURL正在工作,没有问题。有什么想法吗?
以下代码:
import requests
r = requests.post("https://testapp-f55e1.firebaseio.com/test.json", data={"location":{"altitude":"200","latitude":"23.2", "longitude":"44.32"},"polution":{"pm10":"11","pm2":"123"}})
logging.debug(r)
它开始起作用,但没有任何反应。
INFO:Posting to https://testapp-f55e1.firebaseio.com/test.json
请求未到达Firebase。
如果我使用相同的URL执行卷曲请求,它就像魅力一样。有什么想法吗?
答案 0 :(得分:2)
它需要 json 所以用 json = 替换 data = ,请求将调用 json.dumps 并设置你的标题:
In [6]: import requests
...: r = requests.post("https://testapp-f55e1.firebaseio.com/test.json", json
...: ={"location":{"altitude":"200","latitude":"23.2", "longitude":"44.32"},"
...: polution":{"pm10":"11","pm2":"123"}})
...: print(r)
...: print(r.json())
...:
<Response [200]>
{'name': '-KTpRAvBqP4Ra-FSXtKO'}
使用data =的输出给你一个线索:
In [7]: import requests
...: r = requests.post("https://testapp-f55e1.firebaseio.com/test.json", data
...: ={"location":{"altitude":"200","latitude":"23.2", "longitude":"44.32"},"
...: polution":{"pm10":"11","pm2":"123"}})
...: print(r)
...: print(r.json())
...:
<Response [400]>
{'error': "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."}