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.)
-
do I understand correctly that you only want to validate the request parameter "id"? If so, what makes your ID valid? How do you identify a valid ID?st.huber– st.huber2021-10-08 17:15:54 +00:00Commented Oct 8, 2021 at 17:15
-
@st.huber yeah so in my lambda function i first check whether i am getting an id and then i check data related to that id in my DB, if its not there, its an invalid id. But the question that i have is what all validations can i do for GET methodgeek– geek2021-10-11 05:36:40 +00:00Commented Oct 11, 2021 at 5:36
-
you don't have to validate that an id gets passed. API Gateway will handle that for you.st.huber– st.huber2021-10-11 06:26:15 +00:00Commented Oct 11, 2021 at 6:26
-
oh okay, apart from that what other validations can be done for a get method ? Thanks for responding , means a lotgeek– geek2021-10-11 08:34:39 +00:00Commented Oct 11, 2021 at 8:34
1 Answer
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