passing query params for aws lambda function

后端 未结 1 684
时光说笑
时光说笑 2021-01-19 10:40

I am trying to set up a Lambda function that will pull query params that are passed into the API Gateway URL that is created. (Sidebar: I am still pretty green when it comes

1条回答
  •  一生所求
    2021-01-19 10:48

    You need to setup a mapping template in API Gateway. If you know the name of your parameters ahead of time your template could look like this:

    {
      "userId": "$input.params('userId')",
      "limit": "$input.params('limit')",
      "offset": "$input.params('offset')"
    }
    

    Where each $input.params('...') will get evaluated and the value in your query string will be put in its place when the event is passed to Lambda.

    If you don't know the parameter names ahead of time you will have to do some parsing in Lambda. Your mapping template would look like this:

    {
      "querystring": "$input.params().querystring"
    }
    

    Which will end up looking like this in the event that is passed to Lambda:

    {
      "querystring": "{limit=25, offset=0, userId=212733}"
    }
    

    And then you would parse event.querystring instead of window.location.search in your getParam(). Obviously you would need to change some of the logic since you'll be splitting on commas instead of ampersands and you'd need to get rid of the curly brackets. Which by the way, since you are on the server at this point you don't have a window object.

    0 讨论(0)
提交回复
热议问题