Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

566
Visualizações
FAST API- error-value is not valid integer

Doing a tutorial to create a rest API and my get("posts/latest") isn't working. The tutorial says it could be because fast API reads from top to bottom and the it thinks the path is the (posts/{id}) path. I understand that, however: I have moved it to be ahead of that function and i'm still getting the error?

    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}  
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I bet you just forgot starting slash in path name, so the path doesn't match

Following

@app.get("posts/latest")

Should have slash at the beginning of the path

@app.get("/posts/latest")
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda