Compare commits
2 Commits
b8e9dc2ae7
...
d91d177fa9
| Author | SHA1 | Date | |
|---|---|---|---|
| d91d177fa9 | |||
| fa455aaa33 |
162
Docs/Services/FastAPI/05-Put-Method.md
Normal file
162
Docs/Services/FastAPI/05-Put-Method.md
Normal file
@@ -0,0 +1,162 @@
|
||||
# FastAPI – PUT Method
|
||||
|
||||
This document demonstrates how to use the **HTTP PUT method** in FastAPI to update an existing resource.
|
||||
It builds on previous GET and POST examples and completes a basic CRUD-style workflow.
|
||||
|
||||
---
|
||||
|
||||
## Example Application
|
||||
|
||||
Create or update `main.py` with the following content:
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
users = [
|
||||
{"name": "abbas", "age": 20},
|
||||
{"name": "mmd", "age": 37},
|
||||
{"name": "asghar", "age": 19},
|
||||
]
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def home_page():
|
||||
return {"msg": "API is working"}
|
||||
|
||||
|
||||
@app.get("/users")
|
||||
def show_users():
|
||||
return users
|
||||
|
||||
|
||||
@app.post("/create_user")
|
||||
def create_user(name: str, age: int):
|
||||
new_user = {"name": name, "age": age}
|
||||
users.append(new_user)
|
||||
return {"msg": f"user {name} with age {age} created"}
|
||||
|
||||
|
||||
@app.put("/update_user/{target_name}")
|
||||
def update_user(target_name: str, age: int):
|
||||
for user in users:
|
||||
if user["name"] == target_name:
|
||||
user["age"] = age
|
||||
return {"msg": f"user {target_name} updated"}
|
||||
return {"msg": "user not found"}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Code Overview
|
||||
|
||||
### PUT Endpoint Definition
|
||||
|
||||
```python
|
||||
@app.put("/update_user/{target_name}")
|
||||
def update_user(target_name: str, age: int):
|
||||
```
|
||||
|
||||
* Registers an HTTP **PUT** endpoint
|
||||
* `target_name` is a **path parameter**
|
||||
* `age` is a **query parameter**
|
||||
* Used to update an existing user’s data
|
||||
|
||||
---
|
||||
|
||||
### Update Logic
|
||||
|
||||
```python
|
||||
for user in users:
|
||||
if user["name"] == target_name:
|
||||
user["age"] = age
|
||||
```
|
||||
|
||||
* Iterates over the in-memory users list
|
||||
* Matches user by name
|
||||
* Updates the `age` field in place
|
||||
|
||||
---
|
||||
|
||||
### Success Response
|
||||
|
||||
```json
|
||||
{
|
||||
"msg": "user abbas updated"
|
||||
}
|
||||
```
|
||||
|
||||
Returned when the target user exists and is updated successfully.
|
||||
|
||||
---
|
||||
|
||||
### Failure Response
|
||||
|
||||
```json
|
||||
{
|
||||
"msg": "user not found"
|
||||
}
|
||||
```
|
||||
|
||||
Returned when no matching user exists.
|
||||
|
||||
---
|
||||
|
||||
## Example Requests
|
||||
|
||||
### Update User Age (PUT)
|
||||
|
||||
```bash
|
||||
curl -X PUT "http://localhost:8000/update_user/abbas?age=25"
|
||||
```
|
||||
|
||||
### Verify Update
|
||||
|
||||
```http
|
||||
GET /users
|
||||
```
|
||||
|
||||
Updated response:
|
||||
|
||||
```json
|
||||
[
|
||||
{"name": "abbas", "age": 25},
|
||||
{"name": "mmd", "age": 37},
|
||||
{"name": "asghar", "age": 19}
|
||||
]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Running the Application
|
||||
|
||||
Start the service using `uvicorn`:
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## HTTP Method Summary
|
||||
|
||||
| Method | Endpoint | Purpose |
|
||||
| ------ | --------------------- | ----------------------- |
|
||||
| GET | `/` | Health check |
|
||||
| GET | `/users` | Retrieve all users |
|
||||
| POST | `/create_user` | Create a new user |
|
||||
| PUT | `/update_user/{name}` | Update an existing user |
|
||||
|
||||
---
|
||||
|
||||
## Best Practices
|
||||
|
||||
* Use **PUT** for full updates and **PATCH** for partial updates
|
||||
* Return proper HTTP status codes (`404`, `200`, `201`)
|
||||
* Avoid using in-memory data stores in production
|
||||
* Use Pydantic models for request bodies instead of query parameters
|
||||
* Add input validation and error handling
|
||||
* Separate routes, services, and models as the project grows
|
||||
* Make PUT operations idempotent
|
||||
|
||||
172
Docs/Services/FastAPI/06-Delete-Method.md
Normal file
172
Docs/Services/FastAPI/06-Delete-Method.md
Normal file
@@ -0,0 +1,172 @@
|
||||
# FastAPI – DELETE Method (Remove Resource)
|
||||
|
||||
This document demonstrates how to use the **HTTP DELETE method** in FastAPI to remove an existing resource.
|
||||
It completes the CRUD workflow using **GET, POST, PUT, and DELETE** operations.
|
||||
|
||||
---
|
||||
|
||||
## Example Application
|
||||
|
||||
Create or update `main.py` with the following content:
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
users = [
|
||||
{"name": "abbas", "age": 20},
|
||||
{"name": "mmd", "age": 37},
|
||||
{"name": "asghar", "age": 19},
|
||||
]
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def home_page():
|
||||
return {"msg": "API is working"}
|
||||
|
||||
|
||||
@app.get("/users")
|
||||
def show_users():
|
||||
return users
|
||||
|
||||
|
||||
@app.post("/create_user")
|
||||
def create_user(name: str, age: int):
|
||||
new_user = {"name": name, "age": age}
|
||||
users.append(new_user)
|
||||
return {"msg": f"user {name} with age {age} created"}
|
||||
|
||||
|
||||
@app.put("/update_user/{target_name}")
|
||||
def update_user(target_name: str, age: int):
|
||||
for user in users:
|
||||
if user["name"] == target_name:
|
||||
user["age"] = age
|
||||
return {"msg": f"user {target_name} updated"}
|
||||
return {"msg": "user not found"}
|
||||
|
||||
|
||||
@app.delete("/delete_user/{target_name}")
|
||||
def delete_user(target_name: str):
|
||||
for user in users:
|
||||
if user["name"] == target_name:
|
||||
users.remove(user)
|
||||
return {"msg": f"user {target_name} deleted"}
|
||||
return {"msg": "user not found"}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Code Overview
|
||||
|
||||
### DELETE Endpoint Definition
|
||||
|
||||
```python
|
||||
@app.delete("/delete_user/{target_name}")
|
||||
def delete_user(target_name: str):
|
||||
```
|
||||
|
||||
* Registers an HTTP **DELETE** endpoint
|
||||
* `target_name` is a **path parameter**
|
||||
* Used to remove a user from the data store
|
||||
|
||||
---
|
||||
|
||||
### Delete Logic
|
||||
|
||||
```python
|
||||
for user in users:
|
||||
if user["name"] == target_name:
|
||||
users.remove(user)
|
||||
```
|
||||
|
||||
* Iterates through the users list
|
||||
* Finds a matching user by name
|
||||
* Removes the user from the list
|
||||
|
||||
---
|
||||
|
||||
### Success Response
|
||||
|
||||
```json
|
||||
{
|
||||
"msg": "user abbas deleted"
|
||||
}
|
||||
```
|
||||
|
||||
Returned when the user is successfully removed.
|
||||
|
||||
---
|
||||
|
||||
### Failure Response
|
||||
|
||||
```json
|
||||
{
|
||||
"msg": "user not found"
|
||||
}
|
||||
```
|
||||
|
||||
Returned when the specified user does not exist.
|
||||
|
||||
---
|
||||
|
||||
## Example Requests
|
||||
|
||||
### Delete a User
|
||||
|
||||
```bash
|
||||
curl -X DELETE "http://localhost:8000/delete_user/abbas"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Verify Deletion
|
||||
|
||||
```http
|
||||
GET /users
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
[
|
||||
{"name": "mmd", "age": 37},
|
||||
{"name": "asghar", "age": 19}
|
||||
]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Running the Application
|
||||
|
||||
Start the service with `uvicorn`:
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## CRUD Endpoint Summary
|
||||
|
||||
| Method | Endpoint | Description |
|
||||
| ------ | --------------------- | ------------------ |
|
||||
| GET | `/` | Health check |
|
||||
| GET | `/users` | Retrieve all users |
|
||||
| POST | `/create_user` | Create a user |
|
||||
| PUT | `/update_user/{name}` | Update a user |
|
||||
| DELETE | `/delete_user/{name}` | Delete a user |
|
||||
|
||||
---
|
||||
|
||||
## Best Practices
|
||||
|
||||
* Use **DELETE** only for resource removal
|
||||
* Return appropriate HTTP status codes (`204`, `404`)
|
||||
* Ensure delete operations are idempotent
|
||||
* Avoid modifying in-memory data in production
|
||||
* Add authentication and authorization for destructive operations
|
||||
* Log delete actions for auditability
|
||||
* Use database transactions when deleting persistent data
|
||||
|
||||
Reference in New Issue
Block a user