My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label response. Show all posts
Showing posts with label response. Show all posts

Thursday, January 30, 2020

Json schema validation for Api Gateway request

Amazon’s API Gateway provides the facilities to map an incoming request’s payload to match the required format of an integration backend. It uses the concept of “models” and “mapping templates” to specify the mapping between the client payload and the server payload.
A model defines the structure of the incoming payload using JSON Schema. The model is an optional, but not required, piece of API Gateway. By providing a model, you make it easier to define the upcoming mapping template that actually does the transformation between the client and server.
For example, we can define a incoming Player model using the following JSON payload.
{
    "id": "AJamal",
    "alias": "Aha Jamal",
    "displayName": "Anwar jamal Faiz",
    "photoUrl": "https://api.example.com/player/AJamal/avatar.png"
}


JSON Schema is a vocabulary allowing you validate JSON documents. The particular JSON payload being validated is called an instance, and the document describing what a valid payload looks like is called the schema.
JSON Schema is used to describe a JSON document, so it helps to understand what, exactly, a JSON document is composed of these primitives:
  • objects: {"field1": "value1", "field2": "value2"}
  • arrays: ["first", "second", "third"]
  • numbers: 42 or 3.1415926
  • strings: "Lorem ipsum dolor sit amet"
  • booleans: true or false
  • null: null
The responsibility of JSON Schema is to describe a JSON document built from the these primitives.
JSON Schema is itself specified using JSON with predefined semantics for keywords and values. Going back to our Player example, we can specify the JSON Schema for the incoming document as follows:
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "id": { "type": "string" },
        "alias": { "type": "string" },
        "displayName": { "type": "string" },
        "photoUrl": { "type": "string" }
    }
}
This JSON Schema follows the Draft 4 specification and simply declares the types expected for each field in the request. 

For more details consult this guide.

Sending Request Body can be other than JSON in API Gateway in AWS

Can you accept a form request e.g. the classic application/x-www-form-urlencoded body content type. Answer is yes.

You could even use application/xml or any other format from your request but you have to tweak some settings.

Default mapping template in response:
{
    "body" : $input.json('$')
}

Now, if you send a typical application/JSON body…

{
    "foo": "bar"
}

you’ll get is a JSON object under the body key:

{
    "body" : {
       "foo": "bar"
    }
}


Now if you send a different format, say some XML you’ll receive the body key as a string:

{
    "body" : "my request body"
}

However, if you do so then you need your lambda to do the parsing further.

And Ah! ofcourse you need to redeploy your Api gateway after above changes.

Cheers :)