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

Azure Static Web Apps数据库连接预览:PUT请求400错误排查

问题:Azure Static Web Apps中Data API Builder的PUT请求返回400错误

配置文件(staticwebapp.database.config.json)

{
    "$schema": "https://github.com/Azure/data-api-builder/releases/latest/download/dab.draft.schema.json",
    "data-source": {
      "database-type": "mssql",
      "options": {
        "set-session-context": false 
      },
      "connection-string": "@env('DATABASE_CONNECTION_STRING')"
    },
    "runtime": {
      "rest": {
        "enabled": true,
        "path": "/rest"
      },
      "graphql": {
        "allow-introspection": true,
        "enabled": true,
        "path": "/graphql"
      },
      "host": {
        "mode": "production",
        "authentication": {
          "provider": "StaticWebApps"
        }
      }
    },
    "entities": {
      "setup": {
        "source": "dbo.Setup",
        "rest": {
          "path": "/setup",
          "methods": ["get", "post", "put", "delete"],
          "enabled": true
        },
        "permissions": [
          {
            "actions": ["create", "read", "update", "delete"],
            "role": "anonymous"
          }
        ]
      },
      "organizationDetail": {
        "source": {
          "object": "dbo.OrganizationDetail",
          "type": "view"
        },
        "rest": {
          "path": "/organization-detail",
          "methods": ["get"],
          "enabled": true
        },
        "permissions": [
          {
            "actions": ["read"],
            "role": "anonymous"
          }
        ]
      }
    }
  }

请求情况

  • GET请求正常:向https://[target-static-app]/data-api/rest/organization-detail发送请求可正常返回结果
  • PUT请求报错:向https://[target-static-app]/data-api/rest/setup/1发送PUT请求,payload为:
{Id: 1, Title: "Test2", PrimaryColor: "#33cc66", SecondaryColor: "#ccff33", OrganizationId: 1}

返回错误:

"error": {
        "code": "BadRequest",
        "message": "Invalid request body. Either insufficient or extra fields supplied.",
        "status": 400
    }

生成的OpenAPI文档(关键部分)

{
    "openapi": "3.0.1",
    "paths": {
        "/setup/Id/{Id}": {
            "put": {
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/setup_NoPK"
                            }
                        }
                    },
                    "required": true
                }
            }
        }
    },
    "components": {
        "schemas": {
            "setup_NoPK": {
                "type": "object",
                "properties": {
                    "Title": {
                        "type": "string",
                        "format": ""
                    },
                    "PrimaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "SecondaryColor": {
                        "type": "string",
                        "format": ""
                    },
                    "OrganizationId": {
                        "type": "number",
                        "format": ""
                    }
                }
            }
        }
    }
}

解决方法

问题出在PUT请求的payload中包含了Id字段:

  1. 从OpenAPI文档可以看到,PUT请求要求使用setup_NoPK schema,该schema不包含主键字段Id
  2. 因为Id已经通过URL路径参数(/setup/1)指定,Data API Builder会用这个路径参数定位要更新的资源,请求体里不需要再传递Id字段,否则会被判定为额外字段,返回400错误

修正后的PUT请求payload应该去掉Id:

{Title: "Test2", PrimaryColor: "#33cc66", SecondaryColor: "#ccff33", OrganizationId: 1}

补充说明:你的setup实体对应SQL表dbo.Setup的主键是Id,Data API Builder会自动生成排除主键的setup_NoPK schema,用于PUT/PATCH这类更新请求,避免请求体和路径参数的主键冲突。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 18:48:09