Add back JSON detection by request body, but to be deprecated.

This commit is contained in:
L. Karkkainen 2023-01-29 03:03:26 +00:00
parent 52ecbb9dc7
commit c0ca55530e

View File

@ -21,6 +21,7 @@ from traceback import extract_tb
from sanic.exceptions import BadRequest, SanicException
from sanic.helpers import STATUS_CODES
from sanic.log import deprecation
from sanic.request import Request
from sanic.response import HTTPResponse, html, json, text
@ -481,6 +482,22 @@ def _guess_renderer(req: Request, fallback: str, base: t.Type[BaseRenderer]) ->
if mediatype == "application/json":
render_format = "json"
# Check for JSON body content (DEPRECATED, backwards compatibility)
if render_format == "auto" and _acceptable(req, "application/json"):
try:
if req.json:
render_format = "json"
deprecation(
"Response type was determined by the JSON content of "
"the request. This behavior is deprecated and will be "
"removed in v24.3. Please specify the format either by\n"
" FALLBACK_ERROR_FORMAT = 'json', or by adding header\n"
" accept: application/json to your requests.",
24.3,
)
except Exception:
pass
# Use render_format if found and acceptable, otherwise fallback to base
renderer = RENDERERS_BY_CONFIG.get(render_format, base)
mediatype = CONTENT_TYPE_BY_RENDERERS[renderer] # type: ignore