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

FastAPI+Azure函数部署至App Service Plan后登录接口422问题

解决Azure App Service Plan部署FastAPI接口时的422请求错误

问题场景

  • 基于FastAPI开发的Azure函数,部署在Serverless Plan时所有接口运行正常
  • 切换至App Service Plan后,前端请求/v1/login接口返回422(实体未处理)错误,但通过Swagger接口文档(domain/doc)请求该接口可正常工作
  • 该接口接受application/x-www-form-urlencoded类型请求,其余接口前端请求均无异常
  • 前端请求代码:
const formData = new URLSearchParams();
formData.append("username", username);
formData.append("password", password);
console.log(formData.toString())
const response = await fetchClient.post<Token>(
  "/v1/login",
  formData.toString(),
  {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  }
);

原因分析

Azure App Service Plan的路由重定向规则与Serverless Plan存在差异:当请求URL不带尾部斜杠时,App Service会自动发起301重定向,将请求转向带尾部斜杠的地址。但重定向过程中,POST请求的请求体可能丢失,或者请求方法被转换为GET,导致FastAPI无法解析原本的表单参数,最终返回422错误。而Swagger文档在发送请求时会自动处理URL的尾部斜杠,因此可以正常触发接口逻辑。

解决方案

在所有前端请求的URL末尾添加尾部斜杠,避免App Service的自动重定向,确保请求体和参数能完整传递到FastAPI接口。

修改后的前端请求代码:

const formData = new URLSearchParams();
formData.append("username", username);
formData.append("password", password);
console.log(formData.toString())
const response = await fetchClient.post<Token>(
  "/v1/login/", // 添加尾部斜杠
  formData.toString(),
  {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  }
);

验证结果

添加尾部斜杠后,前端请求/v1/login/接口可正常获取响应,不再返回422错误,与Serverless Plan下的表现一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 14:43:22