Skip to content
Myr

clarify 415 vs 400 errors for request.json (#5827)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ADITYA SAH · committed by GitHub · parent 25642fd · Browse files · 1 file+5 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+5 added−3 removed

  • other:+5 added−3 removed

1 file+5 added−3 removed

modified:docs/patterns/javascript.rst+5 added-3 removed
134 unmodified lines
135In general, prefer sending request data as form data, as would be used136when submitting an HTML form. JSON can represent more complex data, but137unless you need that it's better to stick with the simpler format. When138sending JSON data, the ``Content-Type: application/json`` header must be139sent as well, otherwise Flask will return a 400 error.139sent as well, otherwise Flask will return a 415 Unsupported Media Type140error.141142.. code-block:: javascript143144 let data = {
99 unmodified lines
244-----------------------245246Use the :attr:`~flask.Request.json` property of the247:data:`~flask.request` object to decode the request's body as JSON. If247the body is not valid JSON, or the ``Content-Type`` header is not set to248``application/json``, a 400 Bad Request error will be raised.248the body is not valid JSON, a 400 Bad Request error will be raised. If249the ``Content-Type`` header is not set to ``application/json``, a 415250Unsupported Media Type error will be raised.251252.. code-block:: python253254 from flask import request
7 unmodified lines