Build a REST API with HTTP proxy integration
- Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway.
- If this is your first time using API Gateway, you see a page that introduces you to the features of the service. Under REST API, choose Build. When the Create Example API popup appears, choose OK.If this is not your first time using API Gateway, choose Create API. Under REST API, choose Build.
- Choose New API.
- Enter a name in API Name.
- Optionally, add a brief description in Description.
- Choose Create API.
For this tutorial, use
ProxyResourceForPetStore
for the API name. - To create a child resource, choose a parent resource item under the Resources tree and then choose Create Resource from the Actions drop-down menu. Then, do the following in the New Child Resource pane.
- Select the Configure as proxy resource option to create a proxy resource. Otherwise, leave it de-selected.
- Type a name in the Resource Name* input text field.
- Type a new name or use the default name in the Resource Path* input text field.
- Choose Create Resource.
- Select Enable API Gateway CORS, if required.
For this tutorial, select Configure as proxy resource. For Resource Name, use the default,
proxy
. For Resource Path, use/{proxy+}
. Select Enable API Gateway CORS.
In the API just created, the API’s proxy resource path of{proxy+}
becomes the placeholder of any of the backend endpoints underhttp://petstore-demo-endpoint.execute-api.com/
. For example, it can bepetstore
,petstore/pets
, andpetstore/pets/{petId}
. TheANY
method serves as a placeholder for any of the supported HTTP verbs at run time. - To use the API Gateway console to test invoking the API, do the following.
- Choose ANY on a proxy resource in the Resources tree.
- Choose Test in the Method Execution pane.
- From the Method drop-down list, choose an HTTP verb supported by the backend.
- Under Path, type a specific path for the proxy resource supporting the chosen operation.
- If required, type a supported query expression for the chosen operation under the Query Strings heading.
- If required, type one or more supported header expressions for the chosen operation under the Headers heading.
- If configured, set the required stage variable values for the chosen operation under the Stage Variables heading.
- If prompted and required, choose an API Gateway-generated client certificate under the Client Certificate heading to the operation to be authenticated by the back end.
- If prompted, type an appropriate request body in the text editor under the Request Body heading.
- Choose Test to test invoking the method.
For this tutorial, use
GET
for Method in place ofANY
, usepetstore/pets
for Path in place of the proxy resource path ({proxy}), andtype=fish
for Query Strings.
- Because the backend website supports the
GET /petstore/pets?type=fish
request, it returns a successful response similar to the following:[ { "id": 1, "type": "fish", "price": 249.99 }, { "id": 2, "type": "fish", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 } ]
If you try to call
GET /petstore
, you get a404
response with an error message ofCannot GET /petstore
. This is because the backend does not support the specified operation. If you callGET /petstore/pets/1
, you get a200 OK
response with the following payload, because the request is supported by the PetStore website.{ "id": 1, "type": "dog", "price": 249.99 }
- To use a browser to call a GET method on a specific resource of the API, do the following.
- If you have not done so, choose Deploy API from the Actions drop-down menu for the API you created. Follow the instructions to deploy the API to a specific stage. Note the Invoke URL that displays on the resulting Stage Editor page. This is the base URL of the API.
- To submit a GET request on a specific resource, append the resource path, including possible query string expressions to the Invoke URL value obtained in the previous step, copy the complete URL into the address bar of a browser, and choose Enter.
For this tutorial, deploy the API to a
test
stage and appendpetstore/pets?type=fish
to the API’s Invoke URL. This produces a URL ofhttps://
.4z9giyi2c1
.execute-api.us-west-2
.amazonaws.com/test
/petstore/pets?type=fishThe result should be the same as returned when you use
TestInvoke
from the API Gateway console.
Comments are closed, but trackbacks and pingbacks are open.