API, AWS, Cloud Computing, Tutorials

8 Mins Read

Deep dive into the AWS API Gateway

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

[showhide type=”qs” more_text=”click here for Detailed Instructions.. ” less_text=”Hide Details” hidden=”yes”]

 

Step 1: Go to your method, you will see the below screen, click on Integration Request.

16

Step 2: You will see the following screen

60

 

Step 3: Click on Mapping Templates

17

Step 4: Click on Add mapping templates

18

Step 5: Write “text/plain” and click on the tick to save

19

Step 6: Click on pencil symbol near Input passthrough and select Mapping Template

 

Step 7: Enter following JSON string

20

Step 8: Save the Template by clicking on the tick.

21

Step 9: Your API will accept the query string and it will be mapped as per our code.

Step 10: You can TEST your API using REST Client. Call your API passing query string.

[/showhide]

 

2.How to pass Headers to the method?

[showhide type=”header” more_text=”click here for Detailed Instructions.. ” less_text=”Hide Details” hidden=”yes”]

Step 1: Go to the Method Request and Click on the HTTP Request Headers

5

Step 2: Click on the Add Header

6

Step 3: Enter Name of the Header parameter

7

 

 

Step 4: Navigate back to the Method Execution Console and click on Integration Request

Step 5: Click on the Mapping Templates

17

Step 6: Click on the Add mapping templates

18

Step 7: Write “application/json” explicitly.

8

Step 8: Click on Input passthrough

 

9

Step 9: Enter the following JSON string

 

Step 10: Save the Template

[/showhide]

 

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

[showhide type=”trans” more_text=”click here for Detailed Instructions.. ” less_text=”Hide Details” hidden=”yes”]

Step 1: Go to Integration Response

29

 

Step 2: Expand the integration response as shown below

30

Step 3: Click on add Mapping templates

31

Step 4: Click on add Mapping templates

32

Step 5: Enter “application/json” and click on tick to save it

10

Step 5: Click on pencil icon to edit the select Mapping Template.Enter the following Code and save

 

33

Step 6: Navigate back to the Method Execution console and click on Method Response

34

 

Step 7: Expand the Method response and  click on a pencil Icon as shown below

35

 

36

 

Step 8: Replace “json” with “xml” and save

37

Note: Again deploy your API to test your API from REST Client.

Step 9: Test the API from the REST client

38

 

You will find the following response. Now check the Content-type in response.

 

39

 

[/showhide]

 

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

[showhide type=”code” more_text=”click here for Detailed Instructions.. ” less_text=”Hide Details” hidden=”yes”]

 

Step 1: On your Method screen click on Method Response.

34

Step 2: Click on Add Response.

11

Step 3: Select the response code 400 and click on the tick to save.

12

Step 4: Click on Integration Response and add an integration response.

29

Step 5: Provide Lambda Error Regex as “Bad Request: .*”. For Method Response, select the Method response status as the code that you have inserted and click on save.

13

Step 6: Now click on an arrow button as shown below and click on mapping templates and add a template.

14

Step 7: Provide “application/json” as a content type and provide following string as a template and save.

Step 8: Enter the below code in your lambda function before the for loop

Step 9: Test your function with the value greater than 3.

 

It will return you custom code with our custom error.

 

15

 16

 

[/showhide]

 

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

[showhide type=”key” more_text=”click here for Detailed Instructions.. ” less_text=”Hide Details” hidden=”yes”]

Step 1: In AWS API Gateway Menu bar click on APIs and select API Keys

17

Step 2: Click on Create API Key

18

Step 3: Provide and description, leave the check-box unchecked and save

19

Step 4: Your API key has been generated, select API and Stage and click on Add.

20

Step 5: You can see your stage has been added to the Stages Enabled.

Step 6: Now navigate to the API Method Execution Console.

Step 7: Click on Method Response

Step 8: Make API Key Required as true

21

Step 9: Now you can call API from the REST client it will give you message that “Not able to access resource”

22

 

Step 10: Now provide API key in headers of the API call as shown below

23

Step 11: You will get the proper response

24

 

[/showhide]

 

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.

Voiced by Amazon Polly

WRITTEN BY CloudThat

CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.

SHARE

Comments

  1. mahesh

    Sep 28, 2016

    Reply

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

  2. abhishek

    Dec 7, 2015

    Reply

    buddy. the links aren’t working in this article.

    • Bhavik Joshi

      Dec 31, 2015

      Reply

      Can you please mention the link where you are facing the problem?

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!