Author: admin

  • 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,…

  • Multiple Parameters

    Mix Path, Query and body parameters¶ First, of course, you can mix Path, Query and request body parameter declarations freely and FastAPI will know what to do. Note Notice that, in this case, the item that would be taken from the body is optional. As it has a None default value. Multiple body parameters¶ In the previous example, the path operations would expect a JSON body with the attributes…

  • Path Parameters and Numeric Validations

    In the same way that you can declare more validations and metadata for query parameters with Query, you can declare the same type of validations and metadata for path parameters with Path. Import Path Info FastAPI added support for Annotated (and started recommending it) in version 0.95.0. If you have an older version, you would get errors when trying…

  • Query Parameters and String Validations

    FastAPI allows you to declare additional information and validation for your parameters. The query parameter q is of type Union[str, None] (or str | None in Python 3.10), that means that it’s of type str but could also be None, and indeed, the default value is None, so FastAPI will know it’s not required. Note FastAPI will know that the value of q is not required because of…

  • Request Body

    When you need to send data from a client (let’s say, a browser) to your API, you send it as a request body. A request body is data sent by the client to your API. A response body is the data your API sends to the client. Your API almost always has to send a response body. But clients don’t necessarily need…