Managing user
requests and server response
17
profile = valid_profiles[username]
profile.firstname = new_names['fname']
profile.lastname = new_names['lname']
profile.middle_initial = new_names['mi']
valid_profiles[username] = profile
return {"message": "successfully updated"}
else:
return {"message": "user does not exist"}
The updated
version of the preceding
update_profile_names()
service declares a
username
path parameter, a
UUID
id query parameter, and an optional
Dict[str, str]
type. With mixed
parameter types, all required parameters should be declared first, followed by default parameters, and
last in the parameter list should be the optional types. Disregarding this ordering rule will generate
a
compiler error.
Request body
A
request body is a body of data in bytes transmitted from a client
to a server through a
POST
,
PUT
,
DELETE
, or
PATCH
HTTP method operation. In FastAPI, a service must
declare a model object to
represent and capture this request body to be processed for further results.
To implement a model class for the
request body,
you should first import the
BaseModel
class
from the
pydantic
module. Then, create a subclass of it to utilize all the properties and behavior
needed by the path operation in capturing the request body. Here are some
of the data models used
by our application:
from pydantic import
Chia sẻ với bạn bè của bạn: