Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ApiGateway customize Request Validation Response

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?

like image 474
Philipp Avatar asked Feb 01 '18 13:02

Philipp


People also ask

How do I pass AWS API gateway request body?

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.


2 Answers

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.

like image 169
tgk Avatar answered Oct 27 '22 00:10

tgk


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). enter image description here

like image 44
I Bajwa PHD Avatar answered Oct 27 '22 01:10

I Bajwa PHD