即使 POST 存在,POST 上的 Flask 405

jat*_*jat 0 python flask web

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

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


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

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

Aja*_*234 6

methods参数值应在包装路线设置。此外,检查请求是否是POST第一个通常更清晰:

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