Category: Fast API
-
JSON Compatible Encoder
There are some cases where you might need to convert a data type (like a Pydantic model) to something compatible with JSON (like a dict, list, etc). For example, if you need to store it in a database. For that, FastAPI provides a jsonable_encoder() function. Using the jsonable_encoder Let’s imagine that you have a database fake_db that only receives JSON compatible data. For example,…
-
Path Operation Configuration
Response Status Code You can define the (HTTP) status_code to be used in the response of your path operation. You can pass directly the int code, like 404. That status code will be used in the response and will be added to the OpenAPI schema. Technical Details You could also use from starlette import status. FastAPI provides the same starlette.status as fastapi.status just as a convenience for…
-
Handling Errors
There are many situations in which you need to notify an error to a client that is using your API. This client could be a browser with a frontend, a code from someone else, an IoT device, etc. You could need to tell the client that: In these cases, you would normally return an HTTP status…
-
Request Forms and Files
Import File and Form Define File and Form parameters The files and form fields will be uploaded as form data and you will receive the files and form fields.
-
Request Files
Import File¶ Define File Parameters¶ Info File is a class that inherits directly from Form. But remember that when you import Query, Path, File and others from fastapi, those are actually functions that return special classes. Tip To declare File bodies, you need to use File, because otherwise the parameters would be interpreted as query parameters or body (JSON) parameters. The files will be uploaded as…
-
Extra Models
Continuing with the previous example, it will be common to have more than one related model. This is especially the case for user models, because: Danger Never store user’s plaintext passwords. Always store a “secure hash” that you can then verify. If you don’t know, you will learn what a “password hash” is in the security…
-
Cookie Parameters
Import Cookie Declare Cookie parameters¶ Then declare the cookie parameters using the same structure as with Path and Query.
-
Extra Data Types
Up to now, you have been using common data types, like: But you can also use more complex data types. And you will still have the same features as seen up to now: Other data types Here are some of the additional data types you can use: Example¶
-
Nested Models
With FastAPI, you can define, validate, document, and use arbitrarily deeply nested models (thanks to Pydantic). List fields¶ This will make tags be a list, although it doesn’t declare the type of the elements of the list. List fields with type parameter¶ But Python has a specific way to declare lists with internal types, or “type parameters”: Import…
-
Fields
The same way you can declare additional validation and metadata in path operation function parameters with Query, Path and Body, you can declare validation and metadata inside of Pydantic models using Pydantic’s Field. Import Field Warning Notice that Field is imported directly from pydantic, not from fastapi as are all the rest (Query, Path, Body, etc). Declare model attribute Field works the same way as Query, Path and Body, it has all the same parameters,…