FastAPI Flashcards

1
Q

What FastAPI uses behind the scenes to check type validation?

A

Pydantic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an Enum in FastAPI?

A

An Enum in FastAPI is a symbolic name for a set of values, which can be used to define a limited set of valid options for parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False: Enums can be used in FastAPI path parameters to enforce valid input values.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Fill in the blank: Enums in FastAPI can be defined using the _______ module from the Python standard library.

A

enum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following is a valid way to define an Enum in FastAPI? A) class MyEnum(Enum): A = ‘value1’ B) my_enum = Enum(‘MyEnum’, ‘value1 value2’) C) Both A and B

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you specify an Enum as a path parameter in FastAPI?

A

By using the Enum type as the type hint for the path parameter in the route function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What HTTP status code indicates a successful request in FastAPI?

A

200 OK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: In FastAPI, you can set a custom status code in the response using the ‘status_code’ parameter.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Fill in the blank: To return a ‘404 Not Found’ status in FastAPI, you can use the response model and set the status code with ______.

A

status.HTTP_404_NOT_FOUND

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which FastAPI function is used to create a response with a specific status code?

A

JSONResponse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the correct way to return a ‘201 Created’ status when creating a new resource in FastAPI?

A

return JSONResponse(content={‘message’: ‘Resource created’}, status_code=status.HTTP_201_CREATED)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the primary purpose of tags in FastAPI?

A

To group and organize API endpoints for better documentation and usability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False: Tags in FastAPI can only be applied to functions and not to classes.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Fill in the blank: In FastAPI, you can assign tags to an endpoint using the ______ parameter.

A

tags

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which of the following is a correct way to define tags for an endpoint in FastAPI?
A) tags=[‘user’]
B) tag=’user’
C) tags=’user’
D) tag=[‘user’]

A

A) tags=[‘user’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What effect do tags have on the generated OpenAPI documentation in FastAPI?

A

Tags help organize the endpoints into sections, making the documentation clearer and easier to navigate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is FastAPI primarily used for?

A

FastAPI is primarily used for building APIs with Python.

18
Q

True or False: FastAPI automatically validates request and response data.

19
Q

Fill in the blank: FastAPI is based on __________ and uses standard Python type hints.

20
Q

Which feature of FastAPI helps in automatic generation of API documentation?

21
Q

What is the primary benefit of using FastAPI over other frameworks?

A

FastAPI offers high performance and easy-to-use features like automatic data validation and interactive documentation.

22
Q

What is the purpose of the summary feature in FastAPI?

A

The summary feature provides a brief description of the endpoint’s functionality in the generated API documentation.

23
Q

True or False: The description feature in FastAPI can be used to provide detailed information about an endpoint.

24
Q

Fill in the blank: In FastAPI, the summary and description can be added to an endpoint using the parameters ______ and ______.

A

summary, description

25
Which of the following is NOT a benefit of using summary and description features in FastAPI? A) Improved documentation B) Enhanced security C) Clearer API understanding D) Better usability
B) Enhanced security
26
What type of information is typically included in the description feature of a FastAPI endpoint?
Detailed information about the endpoint's purpose, usage, and any relevant details for users.
27
What is the primary purpose of routers in FastAPI?
To define and organize the application's endpoints and their associated HTTP methods.
28
True or False: In FastAPI, routers can be used to group related routes together.
True
29
Fill in the blank: In FastAPI, a router is an instance of the ______ class.
APIRouter
30
Which method is used to include a router in a FastAPI application?
app.include_router()
31
What is a key benefit of using routers in FastAPI for large applications?
They help in organizing code into modular components, making it easier to manage and maintain.
32
What is parameter metadata in FastAPI used for?
It is used to define additional information about parameters in API endpoints.
33
True or False: Parameter metadata can include descriptions, examples, and default values.
True
34
Fill in the blank: In FastAPI, parameter metadata can be specified using the _______ function.
Query
35
Which of the following is NOT a type of parameter metadata in FastAPI? A) description B) example C) validation D) response
D) response
36
What is the purpose of using the 'title' attribute in parameter metadata?
It provides a human-readable name for the parameter in the generated documentation.
37
What is the primary purpose of validators in FastAPI?
To ensure that the data received in requests meets specified criteria before processing.
38
True or False: FastAPI uses Pydantic for data validation.
True
39
Fill in the blank: In FastAPI, a _______ is a class used to define the structure and validation rules for request data.
Pydantic model
40
Which of the following is NOT a type of validation provided by FastAPI? (A) Length validation, (B) Regex validation, (C) SQL injection prevention
C
41
What method is used to validate a request body in FastAPI?
By declaring a Pydantic model as a parameter in the path operation function.