[QUESTION] Why status 422 instead of 400 when invalid (header, query, etc.) parameters in the request?
See original GitHub issueDescription
In the case of an invalid parameter in the request (e.g. header, query, etc.) fastapi returns a 422 status code with the following example body:
{
"detail": [
{
"loc": [
"header",
"foo-bar"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
Firstly, I was wondering what are the reasons for such choice instead of the most common 400 status code. It seems to me a bit strange, especially in the case where a required header is missing, as I would think this classifies as a syntactic error (from description of 422 versus 400 codes).
As such, is there any easy way to change default return http code on validation errors, while ideally keeping the same return body whose structure is pretty neat, instead of defining a custom schema as kind of described in issue #429 ?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:10 (3 by maintainers)
Top Results From Across the Web
400 vs 422 response to POST of data
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived...
Read more >HTTP Status Codes For Invalid Data: 400 vs. 422
A "400 Bad Request" status code indicates that the http request could not be understood - this is pretty much only related to...
Read more >How To Fix the HTTP 422 Error
Error 422 is an HTTP code that tells you that the server can't process your request, although it understands it. The full name...
Read more >422 Unprocessable Entity Explained - KeyCDN Support
The 422 Unprocessable Entity status code is used by those supporting WebDAV and who encounter a semantically incorrect client request.
Read more >RFC 7231: Hypertext Transfer Protocol (HTTP/1.1)
1. 400 Bad Request The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The difference between 422 and 400 becomes a bit subjective.
The rationale is that the error is not about using the protocols and languages incorrectly (HTTP, JSON) nor about doing something otherwise not allowed or incorrect (like performing an operation without enough privileges), but about sending invalid contents, even though using the correct content format (JSON) through the correct channel (valid HTTP).
But I acknowledge that the election between 422 vs others is interpretable and subjective.
The second reason is that Flask-apispec with Marshmallow used 422 too, I thought that was a good idea.
And the other advantage is that you can then separate a whole class of errors, validation errors, that are created automatically by your FastAPI app. And handle them all in a consistent way in your client, without having to do some extra inspections of the contents to decide if it’s a validation error or not.
And then you can raise 400 errors in your own code, and know that those 400 errors will be something specific that you raise, and handle them in a different, custom way in your client.
You can override the validation errors including the status code, here are the docs: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptions
@tiangolo The only issue with this approach in the docs is that auto-generated documentation doesn’t reflect the updated status code. The second issue, perhaps a more pedantic one, is that I also have to define the response structure again, when all I want to do is change the status code.
There’s a very straightforward way to change the status code in webargs: https://webargs.readthedocs.io/en/latest/advanced.html#returning-http-400-responses
Is there any way we could have a way to define a custom status code for validation exceptions, and have that reflected in the generated docs, without defining and raising custom exceptions?