求助:Google API Gateway路径参数转发至CloudRun时变为查询参数
解决API Gateway路径参数转发异常问题
你的API Gateway配置中,路径参数{id}没有被正确替换到后端服务的路径里,而是被转为查询参数传递,这是因为默认的path_translation行为导致的。可以通过以下两种方式修正配置:
方案一:使用路径追加模式
将后端地址设置为Cloud Run服务的基础URL,同时指定path_translation: "APPEND_PATH_TO_ADDRESS",让API Gateway把请求的完整路径追加到后端地址上:
paths: /reports/results/{id}: get: summary: Get Report Results for specified report id operationId: GetReportResults x-google-backend: address: https://myappapi-asldfjoiewjfv-uc.a.run.app/v1 path_translation: "APPEND_PATH_TO_ADDRESS" parameters: - in: path name: id required: true type: integer security: - api_key: [] responses: '200': description: A successful response schema: type: string
方案二:显式映射路径参数
保留后端地址中的路径参数占位符,通过variables字段将请求路径里的id参数映射到后端地址的对应位置:
paths: /reports/results/{id}: get: summary: Get Report Results for specified report id operationId: GetReportResults x-google-backend: address: https://myappapi-asldfjoiewjfv-uc.a.run.app/v1/reports/results/{id} path_translation: "CONSTANT_ADDRESS" variables: id: "$request.path.id" parameters: - in: path name: id required: true type: integer security: - api_key: [] responses: '200': description: A successful response schema: type: string
原因说明
默认情况下,API Gateway的path_translation属性值为CONSTANT_ADDRESS,该模式会将请求中的路径参数提取为查询参数,而非替换到后端地址的路径占位符中。通过上述两种配置,可强制路径参数被正确注入到后端路径,实现你期望的转发效果。
内容的提问来源于stack exchange,提问作者vivekveeramani
相关产品推荐
相关产品推荐

