diff --git a/slow/slow.py b/slow/slow.py index 5e1a130..ba32f95 100644 --- a/slow/slow.py +++ b/slow/slow.py @@ -3,6 +3,7 @@ import http.client import json import re import urllib.parse +from json.decoder import JSONDecodeError from pathlib import Path from typing import Any, Awaitable, Callable, Optional @@ -57,6 +58,11 @@ class Request: } ) + def json(self) -> dict | None: + try: + return json.loads(self.body) + except JSONDecodeError: + return None async def _default_404_route(request: Request): return "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n404 Not Found contact admin".encode(