FAST API- 错误值不是有效整数
FAST API- error-value is not valid integer
做一个教程来创建休息 API 和我的 get("posts/latest") 不工作。教程说这可能是因为 fast API 从上到下读取并且它认为路径是 (posts/{id}) 路径。不过,我明白:我已将其移至该函数之前,但我仍然收到错误消息?
from random import randrange
from typing import Optional
from fastapi import Body, FastAPI, Response , status
from pydantic import BaseModel
app= FastAPI()
class Post(BaseModel):
title: str
content: str
Published: bool = True
rating: Optional[int] = None
my_post = [{"title": "title of post 1", "content": "content of post 1", "id": 2},{"title": "title of post 2","content":"content of post 2", "id":3}]
def find_post(id):
for p in my_post:
if p["id"] == id:
return p
@app.post("/posts")
def create_post(post: Post):
post_dict= post.dict()
post_dict['id']= randrange(0,10000)
my_post.append(post_dict)
print(post)
return { "new_post": post_dict }
@app.get("/posts")
async def get_a_post():
return {"message": "Welcome to my API"}
@app.get("posts/latest")
def get_latest():
latest_post=my_post[len(my_post-1)]
latest_id= latest_post["id"]
print(latest_post)
return {"post":f"here you go - latest post has an id of { latest_id}"}
@app.get("/posts/{id}")
def get_posts(id: int , respose: Response):
post= find_post(id)
print(post)
return{"here is the post": post}
我敢打赌你只是忘记了路径名中的起始斜杠,所以路径不匹配
正在关注
@app.get("posts/latest")
路径开头应该有斜杠
@app.get("/posts/latest")
做一个教程来创建休息 API 和我的 get("posts/latest") 不工作。教程说这可能是因为 fast API 从上到下读取并且它认为路径是 (posts/{id}) 路径。不过,我明白:我已将其移至该函数之前,但我仍然收到错误消息?
from random import randrange
from typing import Optional
from fastapi import Body, FastAPI, Response , status
from pydantic import BaseModel
app= FastAPI()
class Post(BaseModel):
title: str
content: str
Published: bool = True
rating: Optional[int] = None
my_post = [{"title": "title of post 1", "content": "content of post 1", "id": 2},{"title": "title of post 2","content":"content of post 2", "id":3}]
def find_post(id):
for p in my_post:
if p["id"] == id:
return p
@app.post("/posts")
def create_post(post: Post):
post_dict= post.dict()
post_dict['id']= randrange(0,10000)
my_post.append(post_dict)
print(post)
return { "new_post": post_dict }
@app.get("/posts")
async def get_a_post():
return {"message": "Welcome to my API"}
@app.get("posts/latest")
def get_latest():
latest_post=my_post[len(my_post-1)]
latest_id= latest_post["id"]
print(latest_post)
return {"post":f"here you go - latest post has an id of { latest_id}"}
@app.get("/posts/{id}")
def get_posts(id: int , respose: Response):
post= find_post(id)
print(post)
return{"here is the post": post}
我敢打赌你只是忘记了路径名中的起始斜杠,所以路径不匹配
正在关注
@app.get("posts/latest")
路径开头应该有斜杠
@app.get("/posts/latest")