【问题标题】:Provide multiple path parameters to API Gateway (serverless)向 API Gateway 提供多个路径参数(无服务器)
【发布时间】:2020-10-11 06:19:30
【问题描述】:

我在模块中有一个方法“DB_Update”

此方法需要多个参数作为输入(InputA、InputB 和 InputC)

module.exports.DB_Update = async (event) => 
{

    //extract Parameters from event
    InputA= event.pathParameters.InputA
    InputB= event.pathParameters.InputB
    InputC= event.pathParameters.InputC
   
    // Update Items in DB based on Input
    //...
}

我想通过 API 请求使用 serverlessAWS API Gateway

调用该函数

因此在我的 无服务器 yml 文件中我添加了函数


DB_Update:
    handler: ../DB_Update
    events:
      - http:
          path: DB_Update/{InputA, InputB, InputB}
          method: get

最后我通过 Postman 使用参数调用端点

http://localhost:3000/dev/DB_Update/InputA=9783404163809&InputB=111&InputC=BB

但是,无论我尝试哪种交替,我都无法使其正常工作。要么 yml 不接受输入参数的组合,要么我没有得到事件对象。

因此,如果您能给我一个提示如何完成这项工作,那就太好了。谢谢!

【问题讨论】:

    标签: javascript aws-api-gateway serverless


    【解决方案1】:

    您需要决定是要将参数作为路径参数(例如baseurl/{varA}/{varB}/{varC})还是查询参数(例如baseurl?varA=x&varB=y&varC=z)传递。 This answer 提供了对不同模式的更多见解。

    根据您决定的模式,请求参数应按以下格式包含在serverless.yml 文件中(如果需要,请将字段设置为true,如果可选,请设置为false):

    路径参数

    DB_Update:
        handler: ../DB_Update
        events:
          - http:
              path: DB_Update/{InputA}/{InputB}/{InputC}
              method: get
              request:
                parameters:
                  paths:
                    InputA: true
                    InputB: true
                    InputC: true
    

    查询参数:

    DB_Update:
        handler: ../DB_Update
        events:
          - http:
              path: DB_Update
              method: get
              request:
                parameters:
                  querystrings:
                    InputA: true
                    InputB: true
                    InputC: true
    

    访问无服务器框架文档的this section 以了解更多信息。

    【讨论】:

    • 非常感谢。我彻底查看了您的代码以及推荐的其他来源。他们非常有帮助,我能够修复我的代码
    【解决方案2】:

    上面的答案很好。请注意,您实际上不必在请求路径参数下指定参数。这样就足够了:

    DB_Update:
        handler: ../DB_Update
        events:
          - http:
              path: DB_Update/{InputA}/{InputB}/{InputC}
              method: get
    

    你也可以混合使用路径和查询参数,所以你可以有这样的东西:

        handler: ../DB_Update
        events:
          - http:
              path: DB_Update/{InputD}
              method: get
              request:
                parameters:
                  querystrings:
                    InputA: true
                    InputB: true
                    InputC: true
    

    【讨论】:

      猜你喜欢
      • 2018-10-02
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多