简体   繁体   English

GCP API 网关:无法使用路径参数

[英]GCP API Gateway: Cannot use path params

I'm struggling to pass the path params from my gateway to the actual endpoints.我正在努力将路径参数从我的网关传递到实际端点。

Here is my Open API yaml:这是我打开的 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

When I GET the first path, it works.当我得到第一条路径时,它就起作用了。 But in the second path, there is a path param that I cannot find a way to pass to params to my Cloud Run URL. In the log, I see this https://path-to-my-cloud-run-service/v0/auth/users/type/%60type%60?type=email instead of https://path-to-my-cloud-run-service/v0/auth/users/type/email , and that cause my service to reject due to invalid type.但是在第二条路径中,有一个路径参数,我找不到将参数传递给我的 Cloud Run URL 的方法。在日志中,我看到这个https://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 ,这导致我的服务由于类型无效而拒绝。

What do I need to change in my yaml to make this work?我需要在我的 yaml 中更改什么才能使其正常工作?

Another issue I have is that the GET request gets 400 bad request if I put a json in the body even though I specify that it consumes application/json.我遇到的另一个问题是,如果我在正文中放入 json,即使我指定它使用 application/json,GET 请求也会收到 400 个错误请求。

Found the solution after digging here .这里挖掘后找到了解决方案。

It's the path_transaltion, here is the working yaml:这是 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM