|
4 | 4 | from collections import OrderedDict
|
5 | 5 |
|
6 | 6 | from django.conf import settings
|
| 7 | +from django.http.request import RawPostDataException |
7 | 8 | from django.template.loader import render_to_string
|
8 | 9 | from django.urls import path
|
9 | 10 | from django.utils import timezone
|
@@ -40,19 +41,26 @@ def nav_subtitle(self):
|
40 | 41 | return self.get_stats().get("request_url", "")
|
41 | 42 |
|
42 | 43 | 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 | + |
56 | 64 | self.record_stats(
|
57 | 65 | {
|
58 | 66 | "request_url": request.get_full_path(),
|
|
0 commit comments