在 API Gateway 中针对 REST API 测试使用计划 - Amazon API Gateway

在 API Gateway 中针对 REST API 测试使用计划

为了举例说明,我们将使用在教程:通过导入示例创建 REST API中创建的 PetStore API。假定 API 被配置为使用 API 密钥 Hiorr45VR...c4GJc。以下步骤介绍如何测试使用计划。

测试您的使用计划
  • 使用 GET 查询参数对使用计划中的 API (例如 /pets) 的 Pets 资源 (?type=...&page=...) 发出 xbvxlpijch 请求:

    GET /testStage/pets?type=dog&page=1 HTTP/1.1 x-api-key: Hiorr45VR...c4GJc Content-Type: application/x-www-form-urlencoded Host: xbvxlpijch.execute-api.ap-southeast-1.amazonaws.com X-Amz-Date: 20160803T001845Z Authorization: AWS4-HMAC-SHA256 Credential={access_key_ID}/20160803/ap-southeast-1/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-api-key, Signature={sigv4_hash}
    注意

    必须将此请求提交至 API Gateway 的 execute-api 组件,并在所需的 Hiorr45VR...c4GJc 标头中提供所需的 API 密钥(例如 x-api-key)。

    成功的响应会返回 200 OK 状态代码以及包含来自后端的请求结果的负载。如果您忘记设置 x-api-key 标头或使用不正确的密钥进行设置,则会收到 403 Forbidden 响应。不过,如果您未将方法配置为需要 API 密钥,则可能会收到 200 OK 响应 (无论是否正确设置 x-api-key 标头),并且会绕过使用计划的限制和配额限制。

    有时,当出现使 API Gateway 无法对请求实施使用计划限制或配额的内部错误时,API Gateway 将处理该请求,而不会应用使用计划中指定的限制或配额。不过,它会在 CloudWatch 中记录错误消息 Usage Plan check failed due to an internal error。您可以忽略此类偶尔出现的错误。