【问题标题】:Flask 405 on POST even when POST is presentPOST 上的 Flask 405 即使存在 POST
【发布时间】:2018-04-17 23:48:00
【问题描述】:

这是我的代码,一个非常简单的程序。即使 POST 存在,服务器在 POST 请求时仍显示 405。我试过邮递员,http-prompt,但结果是一样的“405”。在向服务器发送 OPTIONS 请求时,只有 GET、HEAD 和 OPTIONS 显示为允许的方法。即使通过 html 表单的 POST 请求也显示 405,当然因为服务器没有尽管 methods kwarg 中存在 POST,但甚至将 POST 作为允许的方法。

  • 我在 Mac 机器上(High Sierra)
  • 尝试使用 gunicorninbuilt 烧瓶服务器。
  • 尝试使用 Postmanhttp-prompt


@app.route('/')
def index(methods=['GET', 'POST']):
    if request.method == 'GET':
        return render_template('index.html')
    else:
        return 'POST'

index.html 包含一个简单的 HTML 标题。

【问题讨论】:

    标签: python web flask


    【解决方案1】:

    methods 参数值应在路由包装器中设置。此外,首先检查请求是否为POST 通常更干净:

    @app.route('/', methods=['GET', 'POST'])
    def index():
       if flask.request.method == 'POST':
          return 'POST'
       return render_template('index.html')
    

    【讨论】:

    • 感谢您指出,因为一个愚蠢的错误而浪费了时间。
    • @Sehwag 很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2012-08-24
    • 2021-01-13
    • 2011-12-10
    • 2017-08-02
    • 2021-10-17
    相关资源
    最近更新 更多