Cannot access any endpoint apart from localhost in Python FastAPI

Question:

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

Asked By: gosiia

||

Answers:

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

enter image description here

Answered By: JarroVGIT
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.