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

Azure Web App上FastAPI的POST接口报ECONNRESET及请求体解析错误

解决方案

1. 确认FastAPI接口定义正确

确保接口使用Form和UploadFile/File参数接收表单数据和文件,示例代码:

from fastapi import FastAPI, File, Form, UploadFile

app = FastAPI()

@app.post("/transcribe")
async def transcribe(
    song_name: str = Form(...),
    singer: str = Form(...),
    audio_file: UploadFile = File(...)
):
    # 业务逻辑
    return {"message": "File received successfully"}

2. 调整Azure Web App应用设置

在Azure门户的Web App「配置」→「应用程序设置」中添加以下配置:

  • 添加WEBSITE_DISABLE_SCM_SEPARATION=true:禁用SCM分离模式,避免前端代理干扰multipart请求解析。
  • 若启用了HTTP20_ONLY,暂时关闭该设置:部分场景下HTTP/2会导致multipart请求的boundary解析异常。

3. 修正Postman请求配置

  • 不要手动设置Content-Type请求头:选择form-data格式后,Postman会自动生成包含正确boundary的Content-Type,手动设置可能导致boundary不匹配。
  • 确认参数类型:song_name和singer选择「Text」类型,audio_file选择「File」类型并上传正确的音频文件。

4. 排查请求头异常(可选)

添加FastAPI中间件打印请求头,验证Azure转发的请求是否正常:

from fastapi import Request

@app.middleware("http")
async def log_request_details(request: Request, call_next):
    content_type = request.headers.get("content-type", "")
    print(f"Received Content-Type: {content_type}")
    if "multipart/form-data" in content_type:
        try:
            boundary = content_type.split("boundary=")[1]
            print(f"Extracted boundary: {boundary}")
        except IndexError:
            print("Failed to extract boundary from Content-Type")
    response = await call_next(request)
    return response

查看Azure日志流中的输出,确认Content-Type中的boundary是否完整且正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 22:45:03