I have configured my AWS APIGateway to validate the requests according to a JSON schema.
E.g. the path /vehicle, which has the following schema attached:
{
"type":"object",
"properties":{
"licensePlate":{
"pattern":"[A-Za-z]{1,3} [A-Za-z]{1,2} \\d{1,4}",
"type":"string"
},
"vehicleType":{
"type":"string",
"enum":[
"Truck",
"Trailer"
]
}
},
"required":[
"licensePlate",
"vehicleType"
]
}
This works fine. If I submit an invalid request the API responds with a 400 {"message": "Invalid request body"}
. I would like to customize this message, e.g. to
{
"entity": "vehicleType",
"message": "missing"
}
If I take a look at the logs from the Gateway it seems that a similar message is logged (object has missing required properties (["vehicleType"])
). Can I use that one? How can I access it?
Logs:
Execution log for request test-request
Thu Feb 01 13:12:18 UTC 2018 : Starting execution for request: test-invoke-request
Thu Feb 01 13:12:18 UTC 2018 : HTTP Method: POST, Resource Path: /vehicle
Thu Feb 01 13:12:18 UTC 2018 : Method request path: {}
Thu Feb 01 13:12:18 UTC 2018 : Method request query string: {}
Thu Feb 01 13:12:18 UTC 2018 : Method request headers: {}
Thu Feb 01 13:12:18 UTC 2018 : Method request body before transformations: {
"licensePlate": "HH AB 123"
}
Thu Feb 01 13:12:18 UTC 2018 : Request body does not match model schema for content type application/json: [object has missing required properties (["vehicleType"])]
Thu Feb 01 13:12:18 UTC 2018 : Method completed with status: 400
Is this possible with the API Gateway?
In the Mapping Templates area, choose an option for Request body passthrough to configure how the method request body of an unmapped content type will be passed through the integration request without transformation to the Lambda function, HTTP proxy, or AWS service proxy.
Yeah what you want is the $context.error.validationErrorString
Like @Bajwa said -- you need to customize the GatewayReponse template. If you're using cloud formation that looks like this:
"GatewayResponse": {
"Type": "AWS::ApiGateway::GatewayResponse",
"Properties": {
"ResponseParameters": {
"gatewayresponse.header.Access-Control-Allow-Origin": "'*'",
"gatewayresponse.header.Access-Control-Allow-Headers": "'*'"
},
"ResponseTemplates": {
"application/json": "{\"error\":{\"message\":\"$context.error.messageString\",\"errors\":\"$context.error.validationErrorString\"}"
},
"ResponseType": "BAD_REQUEST_BODY",
"RestApiId": {
"Ref": "YouRestApiResource"
},
"StatusCode": "400"
}
}
If you violate the request body validator you'll see something like this:
{
"error": {
"message":" "Invalid request body"",
"errors":"[string \"1\" is too short (length: 1, required minimum: 10)]"
}
It's not perfect -- some of the messaging is vague, would be nice if anyone knows how to for example add the property name that caused the violation.
If the request payload is invalid, you will see the same message:
{
"message": "Invalid request body"
}
API Gateway includes more detail, if the request payload format is valid and parameters format is invalid.
See more detail from below link, especially the bottom part.
Test Basic Request Validation in API Gateway
But you may do some customization in the response message by customizing Gateway responses.
You may customize Bad Request Body 400
.
{
"message":"$context.error.messageString",
"Hint":"Check required number of parameters or parameters allowed type and length."
}
Then you will see message in below format.
{
"message": "Invalid request body",
"Hint": "Check required number of parameters or parameters allowed type and length."
}
See hot to update Body Mapping Template from attached screenshot. (APIGateway/Gateway Responses).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With