Managing user requests and server response
11
Multiple path parameters are acceptable if the leftmost variables are more likely to be filled with values
than the rightmost variables.
In other words, the importance of the leftmost path variables will make
the process more relevant and correct than those on the right. This standard is applied to ensure that
the endpoint URL will not look like other URLs, which might cause some conflicts and confusion.
The following
login_with_token()
service
follows this standard, since
username
is a primary
key
and is as strong as, or even stronger than, its next parameter,
password
.
There is an assurance
that the URL will always look unique every time the endpoint is accessed because
username
will
always be required, as well as
password
:
@app.get("/ch01/login/
{username}
/
{password}
")
def login_with_token(
username: str
,
password:str
,
id: UUID):
if valid_users.get(username) == None:
return {"message": "user does not exist"}
else:
user = valid_users[username]
if user.id == id and checkpw(password.encode(),
user.passphrase):
return user
else:
return {"message": "invalid user"}
Unlike
other web frameworks, FastAPI is not friendly with endpoint URLs that belong to base paths
or top-level domain paths with different subdirectories. This occurrence happens when we have
dynamic URL patterns that look the same as the other fixed endpoint URLs
when assigned a specific
path variable. These fixed URLs are implemented sequentially after these dynamic URLs. An example
of these are the following services:
Chia sẻ với bạn bè của bạn: