google-cloud-platform - GCP API 网关:无法使用路径参数
问题描述
我正在努力将路径参数从网关传递到实际端点。
这是我的开放 API yaml:
swagger: '2.0'
info:
description: |
Blah blah
version: 0.0.1
title: SSAuth
contact:
email: blah@gmail.com
schemes:
- https
produces:
- application/json
paths:
/v0/auth/users/echo:
get:
summary: check the health of api
operationId: healthCheck
consumes:
- application/json
produces:
- application/json
responses:
200:
description: OK
x-google-backend:
address: https://path-to-my-cloud-run-service/v0/auth/users/echo
security:
- api_key: []
/v0/auth/users/type/{type}:
post:
summary: Add a new user to the user
operationId: addUser
consumes:
- application/json
produces:
- application/json
parameters:
- name: type
in: path
description: provider type of the user
required: true
type: string
responses:
400:
description: Invalid input
200:
description: OK
x-google-backend:
address: https://path-to-my-cloud-run-service/v0/auth/users/type/`type`
security:
- api_key: []
securityDefinitions:
api_key:
type: apiKey
name: X-API-Key
in: header
当我得到第一条路径时,它就起作用了。但在第二个路径中,有一个路径参数,我找不到将参数传递给我的 Cloud Run URL 的方法。在日志中,我看到 thishttps://path-to-my-cloud-run-service/v0/auth/users/type/%60type%60?type=email
而不是https://path-to-my-cloud-run-service/v0/auth/users/type/email
,这导致我的服务由于类型无效而被拒绝。
我需要在我的 yaml 中进行哪些更改才能使其正常工作?
我遇到的另一个问题是,如果我将 json 放入正文中,即使我指定它使用 application/json,GET 请求也会收到 400 个错误请求。
解决方案
在这里挖掘后找到了解决方案。
这是 path_transaltion,这是工作 yaml:
swagger: '2.0'
info:
description: |
Blahblah
version: 0.0.1
title: Title
contact:
email: blah@gmail.com
schemes:
- https
produces:
- application/json
paths:
/v0/auth/users/echo:
get:
summary: check the health of api
operationId: healthCheck
consumes:
- application/json
produces:
- application/json
responses:
200:
description: OK
x-google-backend:
address: https://path-to-my-service
path_translation: APPEND_PATH_TO_ADDRESS
security:
- api_key: []
/v0/auth/users/type/{type}:
post:
summary: Add a new user to the user
operationId: addUser
consumes:
- application/json
produces:
- application/json
parameters:
- name: type
in: path
description: provider type of the user
required: true
type: string
responses:
400:
description: Invalid input
200:
description: OK
x-google-backend:
address: https://path-to-my-service
path_translation: APPEND_PATH_TO_ADDRESS
security:
- api_key: []
securityDefinitions:
api_key:
type: apiKey
name: X-API-Key
in: header
推荐阅读
- flutter - 从顶部推送的页面更改后,不会产生 Flutter Bloc 状态
- c# - EventWaitHandle.Set,等待超时和释放线程数
- hlsl - 将像素着色器 asm 转换为 HLSL
- python - 从 TWS IB 提取基本数据的最后一段代码缺失
- javascript - 如何修复错误:null 不是对象(评估 'bleManager.start')?
- arrays - 如何在 Arduino 代码的函数参数中使用向量(数组)?
- python - 仅将熊猫数据框中的最后一行附加到新数据框中
- javascript - JavaScript 中 GUID/UUID 函数的语法解释
- r - 如何将 mutate_at 与 ifelse 一起使用
- kubernetes - 验证集群 - api/v1/nodes: http: 服务器向 HTTPS 客户端提供 HTTP 响应