AWS Lambda代理集成(FastAPI+Mangum)的JSON请求格式是什么?
AWS_PROXY集成下Lambda入站请求JSON格式说明
当你用API Gateway的AWS_PROXY集成搭配Mangum托管FastAPI时,Lambda收到的请求必须符合API Gateway的代理集成规范,你的初步格式是基础框架,但还得补全必要字段才能让Mangum正确解析并匹配FastAPI路由。
针对你提供的/get-student/{student_id}接口,完整可用的入站请求JSON示例如下:
{ "httpMethod": "GET", "path": "/get-student/1", "resource": "/get-student/{student_id}", "requestContext": { "httpMethod": "GET", "path": "/get-student/1" }, "pathParameters": { "student_id": "1" }, "headers": { "Content-Type": "application/json" }, "queryStringParameters": null, "body": null, "isBase64Encoded": false }
核心必填字段解释:
- httpMethod:必须和接口请求方法一致,这里是
GET - path:带实际参数值的完整请求路径,比如
/get-student/1 - resource:对应API Gateway里定义的带参数占位符的资源路径,要和FastAPI的路由定义
/get-student/{student_id}匹配 - pathParameters:解析后的路径参数键值对,Mangum会把这个值映射到FastAPI的
student_id参数上 - requestContext:包含请求上下文,Mangum依赖其中的
httpMethod和path来完成路由匹配 - headers:至少要包含
Content-Type字段,确保Mangum能正确处理请求格式
如果只保留你最初的两个字段,Mangum无法识别路径参数,会导致FastAPI找不到对应的接口。
内容的提问来源于stack exchange,提问作者jbuddy_13
相关产品推荐
相关产品推荐

