Search code examples
pythonamazon-web-servicesaws-lambdaserverless-framework

Validating api using serverless framework


I am using Serverless framework. The method I am using is "GET" but documentation/blogs have examples related to "POST" method. Basically I have cards in my website and each card has id associated with it, so when user clicks on particular card , that id is sent to backend, we fetch data related to that id from database and return it to UI. we can see data related to that card on UI. How can I implement request validation for it ? (do not want to write validation inside lambda function.)


Solution

  • Typically request validation is only really useful for POST requests that send a body formatted in sme way such as JSON. A GET request typically just passes and id within the URL. The path property as a part of the serverless.yml configuration woudl validate the path and id value on its own with no additional work necessary as there is no body to validate. An example of a configuration I mean:

    functions:
      params:
        handler: handler.params
        events:
          - httpApi:
              method: GET
              path: /get/for/any/{param}
    

    In this case, if there is any path other than "/get/for/any/" with a value of some type to match {param} at the end as well it will not trigger the Lambda so it is fully validated already