简体   繁体   English

Python:requests.post不发布

[英]Python: requests.post not posting

I'm trying to post a json string to Google Firebase. 我正在尝试将json字符串发布到Google Firebase。

The same command works without hickups using curl, but if I do that using Python, there's no data in the DB. 相同的命令可以在不使用curl的情况下工作,但是如果我使用Python进行操作,则数据库中没有数据。

Here's the code: 这是代码:

postdata = {"pm10": pm10, "pm2.5": pm25, "lat": gps_data.get('latitude'), "lon": gps_data.get('longitude'), "alt": gps_data.get('speed'), "spd": gps_data.get('altitude2'), "satt": gps_data.get('satellites')}

logging.info(args.url)
logging.info(postdata)


r = requests.post(args.url, data=postdata, headers={'content-type': 'application/json'})

logging.debug(r)

I call the script using: 我使用以下命令调用脚本:

python l.py -u http://blabla.firebaseio.com/test1.json python l.py -u http://blabla.firebaseio.com/test1.json

The output is: 输出为:

INFO:Serial device initialized
INFO:Posting to http://blabla.firebaseio.com/test1.json
INFO:http://blabla.firebaseio.com/test1.json
INFO:{'satt': 10, 'pm10': 30, 'lon': XX.XXXXX, 'pm2.5': 27, 'lat': YY.YYYYY, 'alt': 0.037, 'spd': 336.8}
INFO:PM 10: 30
INFO:PM 2.5: 27
INFO:Latitude: XX.XXXX
INFO:longitude: YY.YYYYY

But when I open the DB, no data. 但是当我打开数据库时,没有数据。

Using curl with the same parameters works. 使用具有相同参数的curl可以正常工作。

Update: 更新:

Logging debug provides this as info: 日志调试提供了以下信息:

DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): blabla.firebaseio.com
DEBUG:requests.packages.urllib3.connectionpool:http://blabla.firebaseio.com:80 "POST /test1.json HTTP/1.1" 404 1571
DEBUG:root:<Response [404]>

So, turns out the issue was with one of the variables being passed as json contained an invalid char. 因此,问题出在,其中一个变量传递为json包含无效的char。

The value was pm2.5, so I just renamed it to pm25. 该值为pm2.5,因此我将其重命名为pm25。

The key is not to have any chars such as . 关键是不要包含任何字符。 - or something of the sort. -或类似的东西。

After that it started working. 之后,它开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM