A FastAPI lab for prototyping ideas, testing features, and deepening API knowledge.
- Automatic input Validation
- Auto-Generated interactive Documentation
- Seamless Integration with Modern Ecosystem (ML/DL libraries, OAuth, JWT, SQL Alchemy, Docker, Kubernetes etc)
uvicorn main:app --reload
fastapi dev main.pycd ML_Project/
fasapi dev app.pyA request body is the portion of an HTTP request that contains data sent by the client to the server. It is typically used to HTTP methods POST or PUT to transmit structured data (e.g. JSON, XML, form-data) for the purpose of creating or updating resources on the server. The server parses the request body to extract the necessary information and perform the intended operation.
GET is like reading a book in a library. Post is like writing a note and handing it to the librarian.
- When you GET, you’re just requesting to see something
- When you POST, you’re sending data to be processed or saved.
| Feature | GET | POST |
|---|---|---|
| Purpose | Retrieve data | Submit data |
| Data location | In URL (query string) | In the request body |
| Visibility | Data is visible in URL | Data is hidden from URL |
| Use for | Reading/fetching resources | Creating/updating resources |
| Bookmarkable | Yes | No |
| Idempotent | Yes (repeating gives same result) | Not necessarily (can cause changes |
GET /search?query=shoes HTTP/1.1- You’re asking: “Hey server, show me all the shoes.”
POST /add-to-cart HTTP/1.1
Body: { “item_id”: 123, “quantity”: 2}- You’re saying: “Hey server, please add this item to my cart.”
- You’re asking the server for something.
- Example: “Show me all blog posts tagged with ‘travel’.”
- You don’t change anything on the server.
- The server responds with that info.
**In the browser: **
- You type in a URL or click a link – that’s a GET request.
- Example:
https://example.com/posts?tag=travel
- You’re sending data to the server to do something with it.
- Example: “Here is my comment on this blog post.”
- The server might save that comment to a database.
**In the browser: **
- You fill out a form and click “Submit” – that’s a POST request.
- Example: You post a review, submit login info, or upload a photo.
- GET is for viewing. (safe, can be cached, can be bookmarked.)
- POST is for doing. (Sends new data, not cached, not bookmarkable.)
These are situations where the client just wants to retrieve data, without making any changes:
- URL:
https://news.com/latest - You want to view the latest news.
- No data is sent except in the URL.
- URL:
https://store.com/search?q=laptop - You are searching for laptop”.
- The search term is passed as a query string.
- URL:
https://site.com/user/john - Just displays John’s profile
- Doesn’t change anything.
- URL:
https://blog.com/posts?page=3 - You’re just asking for page 3 of the blog.
- URL:
https://shop.com/products?category=shoes&price=low - You’re applying filters to what you see.
These are situations where the client is sending data to the server to create, modify, or trigger actions.
- URL:
https://site.com/login - Sends your username and password.
- Server checks and starts your session.
- URL:
https://site.com/signup - Sends your name, email, password, etc.
- Sends message, name, email to the server to be saved or emailed.
- Sends the file data as part of the request body.
- You click “Add to Cart” – the item ID and quantity are sent to the server.
- URL:
https://shop.com/checkout - Sends the cart items, payment detail, shipping info.
- Submits text, maybe with a post ID or thread ID.
| Scenario | Method |
|---|---|
| View a blog post | GET |
| Search for a products | GET |
| Login to a site | POST |
| Sign up for a new account | POST |
| Add an item to cart | POST |
| Submit a contact form | POST |
| View a list of product | GET |
| Filter products by price | GET |
| Post a review | GET |
| Get weather info by city | GET |
| Upload a profile picture | POST |
| View user profile | GET |
These happen when you’re just browsing or searching without changing anything on the server.
- When you go to
https://www.flipkart.com, your browser sends a GET request. - You’re just asking to view the homepage.
- You type “mobile phone” in the search bar and hit Enter.
- URL becomes:
https://www.flipkart.com/search?q=mobile+phones - That’s a GET request with the query
q=mobile+phones - You are just requesting info.
- Apply a filter like “Price: Low to High” or select a brand.
- URL updates to something like:
https://www.flipkart.com/search?q=mobile+phones&sort=price_asc&brand=Samsung - Still a GET request – just showing filtered results.
- Click on a phone to see its details.
- URL:
https://www.flipkart.com/Samsung-galaxy/product-id - Again, it’s just fetching info – so it’s a GET.
These happen when you interact in a way that changes something – like logging in, adding to cart, or checking out.
- You enter your phone/email and password.
- Your browser sends a POST request to
https://www.flipkart.com/api/login - Flipkart checks your credentials and starts a session.
- You click “Add to Cart”.
- A POST request is sent to something like:
https://www.flipkart.com/api/add-to-cart - Data sent:
{ product_id: 12345, quantity: 1 }
- After selecting items, address, and payment, you click “Place Order”.
- A POST request sends your order details to Flipkart’s servers to process.
- You rate a product and write a review.
- Flipkart uses a POST request to store that review in their database.
The Path() function in FastAPI is used to provide, validation rules, and documentation hints for path parameters in your API endpoints.
Title
Description
ge, gt, le, lt
Min_length
Max_length
regex
HTTP status codes are 3-digit number returned by a web server (like FastAPI) to indicate the result of a client's request (like from a browser or API consumer).
LINK
They help the client (browser, fronted, mobile app, etc.) understand:
- whether the request was successful.
- whether something went wrong.
- and what kind of issue occurred (if any).
| 2xx | ✅ Success | The request was successfully recieved and processed |
| 3xx | 🔁 Redirection | Further action needs to be taken (e.g. redirect) |
| 4xx | Something is wrong with the request from the client | |
| 5xx | ❌ Server Error | Something went wrong on the server side |
| 200 OK | Standard success | A GET or POST suceeded |
| 201 Created | Resource created | After a POST that created something |
| 204 No Content | Success, but no data returned | After a DELETE request |
| 400 Bad Unauthorized | Malformed or invalid authentication | Missing field, wrong data type |
| 401 Unauthorized | No/invallid authentication | Login required |
| 403 Forbidden | Authenticated, but no permission | Logged in but not allowed |
| 404 Not Found | Resource doesn't exist | Patient ID not DB |
| 500 Internal Server Error | Generic failure | Something broken the server |
| 502 Bad Gateway | Gateway (like Nginx) failed to reach backend | |
| 503 Service Unavailable | Server is down or overloaded |
HTTPException is a Special built-in exception in FastAPI used to return custom HTTP error responses when something goes wrong your API.
Instead of returning a normal JSON or crashing the Server, you can gracefully raise an error with:
- a proper HTTP stats code (like 404, 400, 403, etc.)
- a custom error message
- (optional) extra header
Query parameter are optional key-value pairs appended to the end of a URL, used to pass additional data to the server in an HTTP request. They are typically employed for operations like filtering , sorting.
searching and pagination without altering the endpoint path itself.
/patients?city=Delhi&sort_by=age
- The
?marks the start of query parameters. - Each parameter is a key-value pair:
key=value - Multiple parameters are separated by
&
In this case:
city=Delhiis query parameter for filteringsort_by=ageis a query parameter for sorting
Query() is a utility function provided by FastAPI to declare, validate, and document query parameters in your API endpoints.
1. Define a Pydantic model that represents the ideal schema of the data.
- This includes the expected fields, their types, and any validation constraints (e.g.
gt=0for positive number).
2. nstantiate the model with raw input data (Usually a dictionary or JSON-like structure).
- Pydantic will Automatically validate the data and coerce it into the correct Python type (if possible).
- If the data doesn't meet the model's requirements, Pydantic raise a
validationError.
3. Pass the validateed model object to functions or use it throughout your codebase.
- This ensures that every part of your program works with clean, type-safe, and logically valid data.
In Python, you use classes to describe objects. Think of a class as a tool you use to create your own data structures that contain information about something; you can then use functions (methods) to perform operations on the data you describe.
- To Create a file
touch <file_name>
# Example: touch Test_Excercise.py- Run python (.py) file:
python file_name.py
- To Create a virtual environment
python -m venv <env_name>
# Example: python -m venv playground- Activate the virtual environment
source <env_name>/bin/activate # for macos
source <env_name>/Scripts/activate # for windows
# Example for macos: source playground/bin/activate
# Example for window bash: source playground/Scripts/activate- Deactivate the virtual environment
deactivate- Delete the environment
rm -rf <env_name>
# Example: rm -rf playground- Useful Bash Tips for Environment Management
List Python virtual environments if stored in a folder (e.g., ~/venvs)
ls ~/venvsFind all virtual environments on your system (search for activate scripts)
find ~ -type f -name "activate"Quick script to list venv env names inside ~/venvs
for dir in ~/venvs/*; do
if [ -f "$dir/bin/activate" ]; then
echo "$(basename "$dir")"
fi
done- To install requirements.txt =
pip install -r requirements.txt - To check install packages =
pip list - To check detailed about package =
pip show package_name - To install package =
pip install package_name - To uninstall package =
pip uninstall package_name - To save all packages of env to a requirements.txt file =
pip freeze > requirements.txt
- To add all file =
git add . - To add any particular file =
git add <file_name> - To commit =
git commit -m "commit message" - To push the code =
git push origin main
Docs:
-
Documentation: https://fastapi.tiangolo.com
-
Source Code: https://github.com/fastapi/fastapi
-
Swagger UI:
localhost:8000/docs
http://127.0.0.1:8000/docs






