Page updated Jan 16, 2024

Test the REST API

Test the API from the terminal

If Guest users have access to your REST API you can test it from the terminal using Curl.

Curl is a command-line tool that lets you transfer data to and from a server using various protocols.

Curl is available in many distributions including Mac, Windows and Linux. Follow the install instructions in the docs.

GET method example

1curl https://a5b4c3d2e1.execute-api.eu-west-2.amazonaws.com/dev/todos

POST method example

1curl -H "Content-Type: application/json" -d '{"name":"todo-1"}' https://a5b4c3d2e1.execute-api.eu-west-2.amazonaws.com/dev/todos

GET method example

1curl https://a5b4c3d2e1.execute-api.eu-west-2.amazonaws.com/dev/todos

POST method example

1curl -H "Content-Type: application/json" -d {\"name\":\"todo-1\"} https://a5b4c3d2e1.execute-api.eu-west-2.amazonaws.com/dev/todos

Important! Testing methods using production endpoints may result in changes to resources that cannot be undone.

Test the API with Amplify Mock

Amplify CLI allows you to quickly test your REST APIs by using the amplify mock function command.

Let's test your new REST API using the route below with HTTP Method GET and path /todos?limit=10 which includes a limit query string parameter.

1GET /todos?limit=10

Important! Testing methods using production endpoints may result in changes to resources that cannot be undone.

Before you continue, edit the file at {project}/amplify/backend/function/todosLambda/src/event.json and replace its content for the purpose of the test.

1{
2 "httpMethod": "GET",
3 "path": "/todos",
4 "queryStringParameters": {
5 "limit": "10"
6 }
7}

Make sure you have saved the changes and run

1amplify mock function todosLambda

Select the following options:

  • Provide the path to the event JSON object relative to {project}/amplify/backend/function/todosLambda src/event.json
1Starting execution...
2EVENT: {"httpMethod":"GET","path":"/todos","queryStringParameters":{"limit":"10"}}
3App started
4
5Result:
6{"statusCode":200,"body":"{\"success\":\"get call succeed!\",\"url\":\"/todos?limit=10\"}","headers":{"x-powered-by":"Express","access-control-allow-origin":"*","access-control-allow-headers":"Origin, X-Requested-With, Content-Type, Accept","content-type":"application/json; charset=utf-8","content-length":"55", "date":"Tue, 18 Aug 2020 16:50:53 GMT","connection":"close"},"isBase64Encoded":false}
7Finished execution.

Test the API with API Gateway console

Let's test your new REST API using the route below with HTTP Method GET and path /todos?limit=10 which includes a limit query string parameter.

1GET /todos?limit=10

Important! Testing methods with the API Gateway console may result in changes to resources that cannot be undone.

1/
2 |_ /todos Main resource. Eg: /todos
3 ANY Includes methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
4 OPTIONS Allow pre-flight requests in CORS by browser
5 |_ /{proxy+} Proxy resource. Eg: /todos/, /todos/id, todos/object/{id}
6 ANY Includes methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
7 OPTIONS Allow pre-flight requests in CORS by browser
  • In the Method Execution pane, in the Client box, choose TEST. Choose the GET method. Add limit=10 to the Query String {todos} field.

  • Choose Test to run the test for GET /todos?limit=10. The following information will be displayed: request, status, latency, response body, response headers and logs.

1Request: /todos?limit=10
2Status: 200
3Latency: 139 ms
4Response Body
5{
6 "success": "get call succeed!",
7 "url": "/todos?limit=10"
8}
9Response Headers
10{"access-control-allow-origin":"*","date":"Tue, 18 Aug 2020 17:36:14 GMT","content-length":"55","access-control-allow-headers":"Origin, X-Requested-With, Content-Type, Accept","x-powered-by":"Express","content-type":"application/json; charset=utf-8","connection":"close"}
11Logs
12Execution log for request 4fc3c0c7-6f9f-4ac3-84d7-205500f39b5f
13Tue Aug 18 17:36:14 UTC 2020 : Starting execution for request: 4fc3c0c7-6f9f-4ac3-84d7-205500f39b5f
14Tue Aug 18 17:36:14 UTC 2020 : HTTP Method: GET, Resource Path: /todos
15Tue Aug 18 17:36:14 UTC 2020 : Method request path: {}
16Tue Aug 18 17:36:14 UTC 2020 : Method request query string: {limit=10}
17Tue Aug 18 17:36:14 UTC 2020 : Method request headers: {}
18Tue Aug 18 17:36:14 UTC 2020 : Method request body before transformations:
19Tue Aug 18 17:36:14 UTC 2020 : Endpoint request URI: https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:664371068953:function:expressLambda-dev/invocations
20Tue Aug 18 17:36:14 UTC 2020 : Endpoint request headers: { X-Amz-Date=20200818T173614Z, X-Amz-Source-Arn=arn:aws:execute-api:eu-west-2:664371068953:s3zmw6fqy5/test-invoke-stage/GET/todos, Accept=application/json, User-Agent=AmazonAPIGateway_s3zmw6fqy5, X-Amz-Security-Token=IQoJb3JpZ2luX2VjEDEaCWV1LXdlc3QtMiJGMEQCIC3KIeR66WhaCBw+eJ+GPhF7y4hz9xC2nN+ARb7T3psyAiBdsoaD9yMfiw2dHWjQM5x7vM11XmToNSGu64mckUQdzSq0AwgaEAEaDDU0NDM4ODgxNjY2MyIMIzObNbCd6QtYwb0IKpEDpHXEzkM2OYq7JfL0U/WbF09KNamodfnifRYwZd/GNOwykykc/zHiU9X0XZPRd+QTnQe/9eoy8DaxBkDgRzQQjTThQWJWadtcfjryTLRKpVeo1UueL+f6DTUDf+URjb0P9CN1gPm+ntZD3LSyAXGwACKG7YMA5/HyeEk [TRUNCATED]
21Tue Aug 18 17:36:14 UTC 2020 : Endpoint request body after transformations: {"resource":"/todos","path":"/todos","httpMethod":"GET","headers":null,"multiValueHeaders":null,"queryStringParameters":{"limit":"10"},"multiValueQueryStringParameters":{"limit":["10"]},"pathParameters":null,"stageVariables":null,"requestContext":{"resourcePath":"/todos","httpMethod":"GET","requestTime":"18/Aug/2020:17:36:14 +0000","path":"/todos","accountId":"EXAMPLE_ID","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1597772174890,"requestId":"4fc3c0c7-6f9f-4ac3-84d7-205500f39b5f","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::664371068953:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.11.820 Linux/4.9.217-0.1.ac.205.84.332.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.252-b09 java/1.8.0_252 v [TRUNCATED]
22Tue Aug 18 17:36:14 UTC 2020 : Sending request to https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:664371068953:function:expressLambda-dev/invocations
23Tue Aug 18 17:36:15 UTC 2020 : Received response. Status: 200, Integration latency: 137 ms
24Tue Aug 18 17:36:15 UTC 2020 : Endpoint response headers: {Date=Tue, 18 Aug 2020 17:36:15 GMT, Content-Type=application/json, Content-Length=443, Connection=keep-alive, sampled=0}
25Tue Aug 18 17:36:15 UTC 2020 : Endpoint response body before transformations: {"statusCode":200,"body":"{\"success\":\"get call succeed!\",\"url\":\"/todos?limit=10\"}","headers":{"x-powered-by":"Express","access-control-allow-origin":"*","access-control-allow-headers":"Origin, X-Requested-With, Content-Type, Accept","content-type":"application/json; charset=utf-8","content-length":"55","date":"Tue, 18 Aug 2020 17:36:14 GMT","connection":"close"},"isBase64Encoded":false}
26Tue Aug 18 17:36:15 UTC 2020 : Method response body after transformations: {"success":"get call succeed!","url":"/todos?limit=10"}
27Tue Aug 18 17:36:15 UTC 2020 : Method response headers: {x-powered-by=Express, access-control-allow-origin=*, access-control-allow-headers=Origin, X-Requested-With, Content-Type, Accept, content-type=application/json; charset=utf-8, content-length=55, date=Tue, 18 Aug 2020 17:36:14 GMT, connection=close, Sampled=0}
28Tue Aug 18 17:36:15 UTC 2020 : Successfully completed execution
29Tue Aug 18 17:36:15 UTC 2020 : Method completed with status: 200