In this video, we are going to enable request validation in API, Gateway Rest API.
We can configure API Gateway to perform basic validation of API requests before proceeding with the
integration request.
We can see that this is the one request.
Start with the method request and continue the integration request.
So before the integration request we can put basic validation under the method request.
When the validation face API gateway immediately fails to request and returns a 400 error response to
the invoker.
This is reduced to unnecessary calls to backend services.
So API Gateway can perform the basic validation.
This enables you and API gateway developer focus on the application specific validations in the back
end for basic validation API Gateway verifies some conditions.
For example, it is conditional request parameters.
You're looking the URI created settings and headers of the incoming request and if there is any error,
it is directly to turn back to response to the client application with the validation error.
And also it is also checked for the payload and JSON secure request methods in the method request.
But information in order to enable basic validation, we can specify validation rules in the request
validator.
Add the validator to the API to request validators and assign the validators to the individual API method.
Let's try with our API gateway example.
Please go to the resource section and come to the product and under the product select the gate method
and you can see the one API gateway request integration.
So we can click the method request now because we put the validation in here in order to avoid to go
the integration request and go to downstream services.
If there is an error in the payload, we can directly turn back to response to collide to avoid to unnecessary
calls to downstream services.
Click this method request page.
This is the place of validation performs before redirecting to request to down system services.
It can be lambda function or something.
Yes.
In the method request page we can select the request later.
You can see in here.
Click this icon to edit the request.
Well later.
And now we can put the well date body.
We can see valid body and query sitting parameters.
We can see the validate queries to improvements and headers.
So I would like to select the request validate body.
So after that, in the get request, we don't have body, so we choose last option B to create a string.
For example, we can validate only query string because this is the get method.
We don't have anybody.
So select the validate query setting parameters and click this update button.
Okay.
Very good.
After that, scroll down and go to your query parameters.
You can see the query.
You are requesting parameters.
If we expand this section, we can add query sitting in here, click this button, and now I'm going
to add a query setting, for example, category.
And.
Check this box.
And we can also add the required for the category your query setting parameters.
Okay.
By this way, API Gateway will validate request before redirecting to the directing to the system services
api gateway check the name parameter is then the category indicator setting parameters.
If there is no category in the query strings, this will return any error.
Okay, so let's test this example.
In order to do that, we can go back to method execution path.
So now we can click this test icon and I'm going to test without any providing query setting.
Now we can provide queries in this box, but I'm not providing.
Now scroll down and click the test button and see the error.
So as you can see that the error comes from the a validation part, the message is missing required
to request parameters, which is the category.
So in order to validate this service, we should provide to category.
Equal one, for example.
Okay.
Now click the test again and see this time this will be a return success response.
So two is 200.
And if you want to test with published URL, we have to deploy and test on the web browser on the postman.
But this is the good way to test on directly API, get a management tool and see how validation works
in our example.
OC As you can see that we have enabled request validation and test in the API, Gateway Rest API.