Response - Change Status Code
The current page still doesn’t have a translation for this language.
But you can help translating it: Contributing.
You probably read before that you can set a default .
But in some cases you need to return a different status code than the default.
But if the data didn’t exist, you want to create it, and return an HTTP status code of “CREATED” 201
.
But you still want to be able to filter and convert the data you return with a response_model
.
For those cases, you can use a parameter.
Use a Response
parameter
You can declare a parameter of type Response
in your path operation function (as you can do for cookies and headers).
And then you can return any object you need, as you normally would (a , a database model, etc).
And if you declared a response_model
, it will still be used to filter and convert the object you returned.
FastAPI will use that temporal response to extract the status code (also cookies and headers), and will put them in the final response that contains the value you returned, filtered by any response_model
.
You can also declare the Response
parameter in dependencies, and set the status code in them. But have in mind that the last one to be set will win.