【问题标题】:Cannot access any endpoint apart from localhost in Python FastAPI在 Python FastAPI 中无法访问除 localhost 之外的任何端点
【发布时间】:2022-08-06 15:51:21
【问题描述】:

我正在学习在 Windows 上使用 FastAPI。 问题是我无法访问任何端点而不是 localhost (\"/\")。

我在位于app 文件夹内的main.py 文件中编写了以下代码(app 文件夹包含__init__.py 文件和main.py 文件):

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

    标签: python api fastapi


    【解决方案1】:

    您的代码工作正常,尝试使用localhost 而不是127.0.0.1

    【讨论】:

    • 请接受答案作为解决方案,以便其他人也可以找到它:) 美好的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2018-01-09
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多