- Python v3.9+
# Windows
python -m venv <venv name>
In the project root:
# Windows (Activate)
venv\scripts\activate
# Windows (Deactivate)
venv\scripts\deactivate
pip install -r requirements.txt
# Using the development server Uvicorn
uvicorn app:app --reload
# Using the FastAPI development mode
fastapi dev app.py
# Using the FastAPI production mode
fastapi run app.py
Among the most commonly used HTTP methods are:
Method | Explanation |
---|---|
GET | Used to retrieve data |
POST | Used to send data to the server |
PUT | Used to update a resource |
DELETE | Used to delete a resource |
Used to obtain information from a REST API, we send a request to our application and it responds by delivering the requested resources.
For the GET method, there are two additional ways to extract data, which are:
- URL Parameters
- Query Parameters
We will see these below.
The GET method cannot have a body, unlike the POST and PUT methods that can have a body.
URL parameters are used to send certain data through the URL to locate resources based on their location.
For example: I want to find a book with ID 3, for this I need to use the GET method and send the ID via URL as follows:
[GET] http://localhost:8000/api/books/3 # with the last part being the ID of the book I want to obtain
Query parameters are a way to send multiple dynamic values via URL to perform more specific queries using the GET method.
For example: Imagine we want to capture a book with the initials of The Lord Of The Rings, we would do the following:
Used to create a resource on the server, typically in our database. We send a body to our REST API, which usually looks like this:
# JSON Format
{
"technology": "FastAPI",
"year": 2018
}
The server takes the data we sent, verifies it, and if it meets the validation standards, it is registered in a database.
Used to update data, or a resource on the server. Let's imagine that we want to update a product that is registered in the database as follows:
{
"name": "Coca-Cola",
"price": 2.00
}
Now, we want to update the price, using the PUT method we would send the information as follows:
# JSON format
{
"name": "Coca-Cola",
"price": 5.00
}
So our database will be updated with the data we are sending.