Python中requests返回200但r.json()报JSONDecodeError求助
使用requests发送POST请求获200响应但JSONDecodeError问题解决建议
问题说明
使用Python的requests库发送POST请求时,得到了Response [200]响应,但调用r.json()方法时触发JSONDecodeError。Chrome开发者工具的Payload标签下存在其他键,已尝试传入animal_ids等参数,目标是获取返回JSON中animals/icons下的comment和title字段。
代码示例
def icons(): data = {"key": api_key, "cookie": cookie, "start_date": date1.strftime('20%y-%m-%dT00:00:00-07:00'), "end_date": date2.strftime('20%y-%m-%dT00:00:00-07:00'), "animal_ids": animal_ids } r = requests.post(url, data=data) return r.json() print(icons())
报错信息
Traceback (most recent call last): File "C:\Users\geiss\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 971, in json return complexjson.loads(self.text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\geiss\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\geiss\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\geiss\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\geiss\postreq.py", line 17, in <module> print(icons()) ^^^^^^^ File "C:\Users\geiss\postreq.py", line 16, in icons return r.json() ^^^^^^^^ File "C:\Users\geiss\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
期望返回的JSON数据
{ "success": true, "error": false, "data": { "animals": { "9488": { "icons": [ { "id": null, "animal_id": "9488", "color_label_template_id": "27", "comment": "Can open gates 12/8/21", "created_at": "1638992225", "created_by": "120", "edited_at": null, "edited_by": null, "is_deleted": "0", "deleted_at": null, "deleted_by": null, "fontawesome_icon_id": "1495", "color_label_template_group_id": null, "title": "Opens Gates", "color": "#ff792f", "capacity": null, "checkout_alert": "0", "checkin_alert": "1", "reservation_creation_alert": "0", "owner_details_alert": "0", "animal_details_alert": "0", "reservation_details_alert": "0", "class": "fa fa-surprise", "is_pro": "0", "name": null, "location_id": null, "animal_color_label_id": "8341", "type": "custom" }, { "id": null, "animal_id": "9488", "color_label_template_id": "38", "comment": "see employee notes", "created_at": "1639781526", "created_by": "137", "edited_at": null, "edited_by": null, "is_deleted": "0", "deleted_at": null, "deleted_by": null, "fontawesome_icon_id": "173", "color_label_template_group_id": null, "title": "Grooming", "color": "#16537e", "capacity": null, "checkout_alert": "0", "checkin_alert": "1", "reservation_creation_alert": "1", "owner_details_alert": "0", "animal_details_alert": "1", "reservation_details_alert": "1", "class": "fa fa-scissors", "is_pro": "0", "name": null, "location_id": null, "animal_color_label_id": "8461", "type": "custom" } ] } } } }
解决步骤
- 排查响应内容:200状态码仅表示请求成功,不代表返回的是JSON。在调用
r.json()前添加print(r.text),查看实际返回的内容,可能是空字符串、HTML页面或其他非JSON格式。 - 核对请求格式:打开Chrome开发者工具查看Payload提交类型:
- 如果是
application/json格式,将requests.post(url, data=data)改为requests.post(url, json=data),requests会自动设置正确的Content-Type请求头。 - 如果是表单格式(
application/x-www-form-urlencoded),确认所有Payload中的键都已包含在data字典中,没有遗漏。
- 如果是
- 检查参数格式:确认
animal_ids的类型是否符合后端要求(字符串/列表),日期格式是否和浏览器提交的完全一致。 - 添加必要请求头:复制Chrome中请求的User-Agent、Referer等头信息,添加到请求中:
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" } r = requests.post(url, data=data, headers=headers) - 添加异常处理:捕获JSONDecodeError时输出响应内容,方便定位问题:
try: return r.json() except requests.exceptions.JSONDecodeError: print("非JSON响应内容:", r.text) return None - 提取目标字段:成功获取JSON后,按层级遍历提取comment和title:
result = icons() if result and result.get("success"): for animal_data in result["data"]["animals"].values(): for icon in animal_data["icons"]: print(f"标题:{icon['title']},评论:{icon['comment']}")
内容的提问来源于stack exchange,提问作者SnazzyPete
相关产品推荐
相关产品推荐

