2018-12-15 14:03:59 +04:00
You can declare the model used for the response with the parameter `response_model` in any of the path operations:
2018-12-14 21:35:19 +04:00
* `@app.get()`
* `@app.post()`
* `@app.put()`
* `@app.delete()`
* etc.
2018-12-18 22:55:30 +04:00
```Python hl_lines="17"
2018-12-21 16:22:33 +04:00
{!./src/response_model/tutorial001.py!}
2018-12-14 21:35:19 +04:00
` ``
!!! note
2018-12-15 14:03:59 +04:00
Notice that ` response_model` is a parameter of the "decorator" method (` get`, ` post`, etc). Not of your path operation function, like all the parameters and body.
2018-12-14 21:35:19 +04:00
It receives a standard Pydantic model and will:
* Convert the output data to the type declarations of the model
* Validate the data
2018-12-15 14:03:59 +04:00
* Add a JSON Schema for the response, in the OpenAPI path operation
2018-12-14 21:35:19 +04:00
* Will be used by the automatic documentation systems
But most importantly:
* Will limit the output data to that of the model. We'll see how that's important below.
2019-03-29 17:02:53 +02:00
!!! note "Technical Details"
The response model is declared in this parameter instead of as a function return type annotation, because the path function may not actually return that response model but rather return a ` dict`, database object or some other model, and then use the ` response_model` to perform the field limiting and serialization.
2018-12-14 21:35:19 +04:00
## Return the same input data
Here we are declaring a ` UserIn` model, it will contain a plaintext password:
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="8 10"
2018-12-21 16:22:33 +04:00
{!./src/response_model/tutorial002.py!}
2018-12-14 21:35:19 +04:00
` ``
And we are using this model to declare our input and the same model to declare our output:
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="16 17"
2018-12-21 16:22:33 +04:00
{!./src/response_model/tutorial002.py!}
2018-12-14 21:35:19 +04:00
` ``
Now, whenever a browser is creating a user with a password, the API will return the same password in the response.
2019-03-02 19:54:52 +04:00
In this case, it might not be a problem, because the user himself is sending the password.
2018-12-14 21:35:19 +04:00
2018-12-29 18:43:58 +04:00
But if we use the same model for another path operation, we could be sending the passwords of our users to every client.
2018-12-14 21:35:19 +04:00
!!! danger
Never send the plain password of a user in a response.
## Add an output model
We can instead create an input model with the plaintext password and an output model without it:
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="8 10 15"
2018-12-21 16:22:33 +04:00
{!./src/response_model/tutorial003.py!}
2018-12-14 21:35:19 +04:00
` ``
2018-12-15 14:03:59 +04:00
Here, even though our path operation function is returning the same input user that contains the password:
2018-12-14 21:35:19 +04:00
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="23"
2018-12-21 16:22:33 +04:00
{!./src/response_model/tutorial003.py!}
2018-12-14 21:35:19 +04:00
` ``
...we declared the ` response_model` to be our model ` UserOut`, that doesn't include the password:
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="21"
2018-12-21 16:22:33 +04:00
{!./src/response_model/tutorial003.py!}
2018-12-14 21:35:19 +04:00
` ``
So, **FastAPI** will take care of filtering out all the data that is not declared in the output model (using Pydantic).
## See it in the docs
When you see the automatic docs, you can check that the input model and output model will both have their own JSON Schema:
<img src="/img/tutorial/response-model/image01.png">
And both models will be used for the interactive API documentation:
<img src="/img/tutorial/response-model/image02.png">
## Recap
2018-12-15 14:03:59 +04:00
Use the path operation decorator's parameter ` response_model` to define response models and especially to ensure private data is filtered out.