16
loading...
This website collects cookies to deliver better user experience
products = [
{"id": 1, "name": "iPad", "price": 599},
{"id": 2, "name": "iPhone", "price": 999},
{"id": 3, "name": "iWatch", "price": 699},
]
POST www.example.com/products
GET www.example.com/products
GET www.example.com/products/2
PUT www.example.com/products/2
DELETE www.example.com/products/2
GET www.example.com/products/2
from fastapi import FastAPI
app = FastAPI()
products = [
{"id": 1, "name": "iPad", "price": 599},
{"id": 2, "name": "iPhone", "price": 999},
{"id": 3, "name": "iWatch", "price": 699},
]
@app.get("/products")
def index():
return products
$ uvicorn first-api:app --reload
[
{
"id": 1,
"name": "iPad",
"price": 599
},
{
"id": 2,
"name": "iPhone",
"price": 999
},
{
"id": 3,
"name": "iWatch",
"price": 699
}
]
@app.get("/products/{id}")
def index(id: int):
for product in products:
if product["id"] == id:
return product
return "Not found"
from fastapi import FastAPI, Response
...
@app.get("/products/{id}")
def index(id: int, response: Response):
for product in products:
if product["id"] == id:
return product
response.status_code = 404
return "Product Not found"
/products/search/?name=iPhone
@app.get("/products/search")
def index(name, response: Response):
founded_products = [product for product in products if name.lower() in product["name"].lower()]
if not founded_products:
response.status_code = 404
return "No Products Found"
return founded_products if len(founded_products) > 1 else founded_products[0]
http://127.0.0.1:8000/products/search?name=iPhone
{
"detail": [
{
"loc": [
"path",
"id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
from fastapi import FastAPI, Response
app = FastAPI ()
products = [
{"id": 1, "name": "iPad", "price": 599},
{"id": 2, "name": "iPhone", "price": 999},
{"id": 3, "name": "iWatch", "price": 699},
]
@app.get("/products")
def index():
return products
@app.get("/products/{id}")
def index(id: int, response: Response):
for product in products:
if product["id"] == id:
return product
response.status_code = 404
return "Product Not found"
@app.get("/products/search")
def index(name, response: Response):
founded_products = [product for product in products if name.lower() in product["name"].lower()]
if not founded_products:
response.status_code = 404
return "No Products Found"
return founded_products if len(founded_products) > 1 else founded_products[0]
from fastapi import FastAPI, Response, responses
app = FastAPI()
products = [
{"id": 1, "name": "iPad", "price": 599},
{"id": 2, "name": "iPhone", "price": 999},
{"id": 3, "name": "iWatch", "price": 699},
]
@app.get("/products")
def index():
return products
@app.get("/products/search")
def index(name, response: Response):
founded_products = [product for product in products if name.lower() in product["name"].lower()]
if not founded_products:
response.status_code = 404
return "No Products Found"
return founded_products if len(founded_products) > 1 else founded_products[0]
@app.get("/products/{id}")
def index(id: int, response: Response):
for product in products:
if product["id"] == id:
return product
response.status_code = 404
return "Product Not found"
{
"id": 2,
"name": "iPhone",
"price": 999
}