如何使用 aws apigateway CLI 指定方法请求路径变量

How to specify method request path variables with aws apigateway CLI

我在 AWS API 网关中定义了一个使用路径变量的路由,可以像这样访问:

/route/{variable}

一切都已正确配置并按我预期的方式工作,只是我找不到如何通过 CLI 测试此路由。当我在该方法上使用 AWS 控制台的 "TEST" 函数时,它会提示我为我的变量输入所需的值。我这样做了,它按预期工作,执行日志中出现以下内容:

Thu Jan 07 16:30:06 UTC 2016 : Method request path: {variable=my specified value}
Thu Jan 07 16:30:06 UTC 2016 : Method request headers: {}

然而,当我使用 CLI 和这个命令执行它时:

$ aws apigateway test-invoke-method --rest-api-id {rest-api-id} --resource-id {resource-id} --http-method GET --path-with-query-string 'variable=my specified value'

我收到 500 ISE 响应,日志中包含以下内容:

Thu Jan 07 16:38:20 UTC 2016 : Method request path: {}
Thu Jan 07 16:38:20 UTC 2016 : Method request headers: {}
Thu Jan 07 16:38:20 UTC 2016 : Execution failed due to configuration error: Unable to transform input

我已经尝试了 许多 这个主题的变体,包括使用 JSON 编码的字符串作为 --path-with-query-string 值(例如 {"variable":"my specified value"}),使用原始路径(例如 /route/my%20specified%20value),以及其他几个。

我还尝试使用 --stage-variables 开关指定此值,并将 --path-with-query-string 值设为空白。这会产生相同的结果。

我已经能够通过指定 --headers '{"variable":"my specified value"}' 来让我的电话工作,但这似乎不正确,因为它绕过了路径变量,所以它不是一个完全有效的测试。有没有办法使用 CLI 指定 Method request path 变量?非常感谢您的帮助。

您需要使用完整路径发出命令,即:

$ aws apigateway test-invoke-method --rest-api-id abc123 --resource-id xyz987 --http-method GET --path-with-query-string '/route/123 '

上面的错误消息表明请求 body 转换出错。您可能需要指定一个 --body 参数,and/or 通过 --headers 参数指定 accept/content-type headers。

如果有帮助请告诉我。

谢谢, 瑞安