Skip to content

Commit 7f58015

Browse files
committed
Reading the request again may not work
Avoids the following exception to crash the process: RawPostDataException: You cannot access body after reading from request's data stream
1 parent a1746bc commit 7f58015

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

debug_toolbar/panels/history/panel.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections import OrderedDict
55

66
from django.conf import settings
7+
from django.http.request import RawPostDataException
78
from django.template.loader import render_to_string
89
from django.urls import path
910
from django.utils import timezone
@@ -40,19 +41,26 @@ def nav_subtitle(self):
4041
return self.get_stats().get("request_url", "")
4142

4243
def generate_stats(self, request, response):
43-
if request.method == "GET":
44-
data = request.GET.copy()
45-
else:
46-
data = request.POST.copy()
47-
# GraphQL tends to not be populated in POST. If the request seems
48-
# empty, check if it's a JSON request.
49-
if not data and request.META.get("CONTENT_TYPE") == "application/json":
50-
# Python <= 3.5's json.loads expects a string.
51-
data = json.loads(
52-
request.body
53-
if sys.version_info[:2] > (3, 5)
54-
else request.body.decode(request.encoding or settings.DEFAULT_CHARSET)
55-
)
44+
try:
45+
if request.method == "GET":
46+
data = request.GET.copy()
47+
else:
48+
data = request.POST.copy()
49+
# GraphQL tends to not be populated in POST. If the request seems
50+
# empty, check if it's a JSON request.
51+
if not data and request.META.get("CONTENT_TYPE") == "application/json":
52+
# Python <= 3.5's json.loads expects a string.
53+
data = json.loads(
54+
request.body
55+
if sys.version_info[:2] > (3, 5)
56+
else request.body.decode(
57+
request.encoding or settings.DEFAULT_CHARSET
58+
)
59+
)
60+
except RawPostDataException:
61+
# It is not guaranteed that we may read the request data (again).
62+
data = None
63+
5664
self.record_stats(
5765
{
5866
"request_url": request.get_full_path(),

0 commit comments

Comments
 (0)