Deep dive into the AWS API Gateway

October 21, 2015 | Comments(4) |

From my previous blog, you can get a hands on with the AWS API Gateway. Now we will deep dive into it. Following points will be covered.

  1. How to pass Query String to the method?
  2. How to pass Headers to the method?
  3. How to Transform the Response of the Method?
  4. How to receive Custom Response Code (400,500,…) for the method?
  5. How partially secure your API using API Keys

Prerequisites:

  1. Create a lambda function with code given here
  2. Create and Deploy the Product API with the GET Method, which integrates with the above function

You can refer my previous blog to have the prerequisites.

 

1.How to pass Query String to the method?

You have an API with you already, where you are calling an API with GET method, you get all the records in response, but what if we want a specific record in response? How can we get it? In this scenario, we can use the Query String. We will get the query string parameter from the HTTP request and then integrate the request using Mapping Templates.

Step by Step process to pass the Query String Parameters to the Method

 

2.How to pass Headers to the method?

 

3.How to Transform the Response of the method?

Converting the response helps when we are returning in JSON, but we need the output in XML. We can achieve the same by transforming the API response. For transforming the response, we will again use mapping templates. We will write a template which converts the response and gives the desired type in return.

 

Step by Step process for transforming the JSON response to XML

 

4.How to receive custom response code (400, 500,…) for the method?

When we are calling any method, by default it returns 200 as a response code in AWS API Gateway. Even though if the response is success or error still the code returned will be 200. AWS API Gateway provides the feature where we can actually give the custom code for the response.

We have an API where we are passing a query string or header values like 1, 2 and 3. In response, we are getting the product detail about the respective product_Id. So we will set the custom code for the BAD REQUEST. If the person is passing the query string value more than 3, we will return an error with the custom code 400.

 

Step by Step process to Receive Custom Response Code from the Method

 

5.How partially secure your API using API Keys

API keys are key generated by the Developer. Developer distributes the key to the third party users to restrict the unwanted user partially as well as to monitoring the API usage of the particular user. I have said that the API key is for the used for the authentication partially because AWS does not recommend to use only API Keys for the authentication.

When we enable the API Keys, the user must pass the Key to call the API. Othervise it will give retuen a message “Not able to access resources”.

 

Step by Step process to call the API using API Key

 

AWS API Gateway provides the service which provides the platform to create REST APIs with minimal efforts.it does not only provides the API calls as well as such a useful features as passing query string and header parameters, transforming the response type, returning the custom error code and using the API Keys for authentication.

The AWS API is becoming the powerful tool to create and deploy the REST APIs with integrating with Lambda function, although you can use the other endpoints as well.It also provides many other features like Cloud watch Monitoring,caching.API Key to distribute to the third party users.

If you have any queries on API gateway or you have trouble getting it setup, feel free to drop a question into the comment section below.


4 Responses to “Deep dive into the AWS API Gateway”

  1. mahesh

    Can you give me an example for get request using lambda?
    e.g. function accepts “Key”: “mykey”
    and returns its “Value”: “its value”

    Reply

Leave a Reply