pythonfastapi

Cannot access any endpoint apart from localhost in Python FastAPI


I am learning to use FastAPI on Windows. The problem is that I cannot access any endpoint rather than localhost ("/").

I have written the following code in main.py file that is located inside app folder (app folder contains __init__.py file and main.py file):

from fastapi import FastAPI

app = FastAPI()

my_posts = { 1:{
        "date":"01/01/2022",
        "user":"Matt",
        "type":"photo"
}
}

@app.get("/")
def get_root():
    return{"Hello":"User"} #works fine


@app.get("/post/{post_id}")
def get_post(post_id:int):
    return{"data": my_posts} #doesn't work

enter image description here


Solution

  • Your code works fine, try using localhost rather than 127.0.0.1:

    enter image description here