You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask服务部署Azure后/upload-data路径报405错误,本地运行正常

解决Flask部署Azure后/upload-data路径POST请求405错误

以下是针对问题的排查和解决步骤:

1. 修复HTTPS重定向导致的请求方法变更

Azure App Service默认强制HTTPS,若移动端使用HTTP发起请求,会被301重定向到HTTPS,此时POST请求会被转换为GET,触发405错误。

解决:
修改Android代码中的URL协议为HTTPS:

String url = "https://" + backendUrl;

同时在Azure门户的应用服务「TLS/SSL设置」中确认「HTTPS 强制」已开启(默认开启),确保所有请求使用HTTPS。

2. 验证Web.config的请求方法配置(IIS托管场景)

若Flask应用通过wfastcgi在IIS上运行,Web.config的handler配置可能限制了请求方法。

解决:
确保Web.config中handlers节点的verb属性设置为*,允许所有HTTP方法:

<handlers>
  <add name="Python FastCGI" path="handler.py" verb="*" modules="FastCgiModule" scriptProcessor="你的Python路径|wfastcgi.py路径" resourceType="Unspecified" requireAccess="Script" />
</handlers>

同时确认rewrite规则没有过滤POST请求,规则示例:

<rewrite>
  <rules>
    <rule name="Flask Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="handler.py/{R:0}" />
    </rule>
  </rules>
</rewrite>

3. 排查Flask路由冲突

确认Flask应用中没有重复定义/upload-data路由,或其他路由覆盖了该路径的POST方法。确保只有一处如下定义:

@app.route("/upload-data", methods=['POST'])
def store_data():
    # 处理逻辑

4. 开启日志排查实际请求情况

在Azure门户开启应用日志,查看请求详情:

  1. 进入应用服务 → 「日志」→ 开启「应用日志(文件系统)」
  2. 在Flask的store_data函数中添加日志,记录请求方法:
import logging
from flask import request

@app.route("/upload-data", methods=['POST'])
def store_data():
    logging.info(f"Received request method: {request.method}")
    # 后续处理

通过日志确认到达Flask的请求方法是否为POST,若不是,说明请求在到达应用前被修改。

内容的提问来源于stack exchange,提问作者Oli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 11:50:05