Managing user requests and server response
Clients can pass their request data to FastAPI endpoint URLs through path parameters, query parameters,
or headers to pursue service transactions. There are standards and ways to use these parameters to
obtain incoming requests. Depending on the goal of the services, we use these parameters to influence
and build the necessary responses the clients need. But before we discuss these various parameter
types, let us explore first how we use type hinting in FastAPI’s local parameter declaration.
Parameter type declaration
All request parameters are required to be type-declared in the method signature of the service method
applying the
PEP 484
standard called type hints. FastAPI supports common types such as
None
,
bool
,
int
, and
float
and container types such as
list
,
tuple
,
dict
,
set
,
frozenset
, and
deque
. Other complex Python types such as
datetime.date
,
datetime.time
,
datetime.
datetime
,
datetime.delta
,
UUID
,
bytes
, and
Decimal
are also supported.
The framework also supports the data types included in Python’s
typing
module, responsible for
type hints. These data types are standard notations for Python and variable type annotations that can
help to pursue type checking and model validation during compilation, such as
Optional
,
List
,
Dict
,
Set
,
Union
,
Tuple
,
FrozenSet
,
Iterable
, and
Deque
.
Path parameters
FastAPI allows you to obtain request data from the endpoint URL of an API through a path parameter
or path variable that makes the URL somewhat dynamic. This parameter holds a value that becomes
Chia sẻ với bạn bè của bạn: |