Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/itsronalds/fastapi
FastAPI Practices
https://github.com/itsronalds/fastapi
Last synced: about 21 hours ago
JSON representation
FastAPI Practices
- Host: GitHub
- URL: https://github.com/itsronalds/fastapi
- Owner: itsronalds
- Created: 2024-09-04T18:41:13.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-17T20:52:54.000Z (about 2 months ago)
- Last Synced: 2024-09-18T02:00:10.003Z (about 2 months ago)
- Language: Python
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# FastAPI Tutorial
# Create a FastAPI Project
## Requirements
- Python v3.9+
## Create Virtual Environment
```$
# Windows
python -m venv
```## Activate/Deactivate Virtual Environment
In the project root:
```$
# Windows (Activate)
venv\scripts\activate# Windows (Deactivate)
venv\scripts\deactivate
```## Install Dependencies
```$
pip install -r requirements.txt
```## Run the Project
```$
# 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
```# HTTP Methods
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 |## Get Method
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 ParametersWe will see these below.
> The **GET** method cannot have a body, unlike the **POST** and **PUT** methods that can have a body.
### URL Parameters
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
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:
## POST Method
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**.
## PUT Method
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.