Anders
Anders

Reputation: 3412

How to get AWS API Gateway to validate JSON Body against Model

The newly released "x-amazon-apigateway-request-validator" (in April I believe) is supposed to be able to run a JSON schema validation against the POST/PUT payload of an API.

I've tried to implement this in several different ways now but I can't make it work... I have added the validators:

"x-amazon-apigateway-request-validators": {
  "ValidateBody": {
    "validateRequestParameters": false,
    "validateRequestBody": true
  },
  "ValidateHeaders": {
    "validateRequestParameters": true,
    "validateRequestBody": false
  }

}

For any methind I will look for specific headers so I also have the parameters in the "root" of the Swagger:

"x-amazon-apigateway-request-validator": "ValidateHeaders"

In the POST I have a x-amazon-apigateway-request-validator: ValidateBody and then a Model with the JSON schema where several elements are required.

I can however POST anything as long as it is JSON... There is no validation against the JSON schema done...

What am I missing?

Upvotes: 1

Views: 2209

Answers (1)

Anders
Anders

Reputation: 3412

It is working! There is a time lag between the deploy and the actual usage of the new code even though I am not doing any caching it seems.

Went out for lunch and came back to a working solution!

{
  "message": "Invalid request body"
}

It would be nice to be able to modify the response message though...

Upvotes: 2

Related Questions