Python:POST请求不起作用?
向Firebase发出简单的POST请求。由于某种原因,它不起作用。使用相同数据的cURL正在工作,没有问题。有什么想法吗 代码如下:Python:POST请求不起作用?,python,curl,firebase,python-requests,Python,Curl,Firebase,Python Requests,向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":
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执行一个curl请求,它就像一个符咒。有什么想法吗?它需要json,因此将data=替换为json=,请求将调用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'}
[6]中的:导入请求
…:r=请求。发布(“https://testapp-f55e1.firebaseio.com/test.json“,json
…:={“位置”:{“高度”:“200”,“纬度”:“23.2”,“经度”:“44.32”},“
…:污染:{“pm10”:“11”,“pm2”:“123})
…:打印(r)
…:打印(r.json())
...:
{'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."}
[7]中的:导入请求
…:r=请求。发布(“https://testapp-f55e1.firebaseio.com/test.json“,数据
…:={“位置”:{“高度”:“200”,“纬度”:“23.2”,“经度”:“44.32”},“
…:污染:{“pm10”:“11”,“pm2”:“123})
…:打印(r)
…:打印(r.json())
...:
{'error':“无效数据;无法分析JSON对象、数组或值。可能您在密钥名中使用了无效字符。”}
它需要json,因此将data=替换为json=,请求将调用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'}
[6]中的:导入请求
…:r=请求。发布(“https://testapp-f55e1.firebaseio.com/test.json“,json
…:={“位置”:{“高度”:“200”,“纬度”:“23.2”,“经度”:“44.32”},“
…:污染:{“pm10”:“11”,“pm2”:“123})
…:打印(r)
…:打印(r.json())
...:
{'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."}
[7]中的:导入请求
…:r=请求。发布(“https://testapp-f55e1.firebaseio.com/test.json“,数据
…:={“位置”:{“高度”:“200”,“纬度”:“23.2”,“经度”:“44.32”},“
…:污染:{“pm10”:“11”,“pm2”:“123})
…:打印(r)
…:打印(r.json())
...:
{'error':“无效数据;无法分析JSON对象、数组或值。可能您在密钥名中使用了无效字符。”}
你怎么知道它不起作用?@TristanT,可能400响应是个不错的选择。我查看了Firebase DB,执行脚本时没有添加新记录。当我键入curl命令时,新记录确实会出现@TristanTHow你知道它不起作用吗?@TristanT,可能400响应是个不错的选择。我查看了Firebase DB,执行脚本时没有添加新记录。当我键入curl命令时,新记录确实会出现@崔斯坦提斯,成功了!是的,成功了!