【问题标题】:Can AWS API Gateway support `application/x-www-form-urlencoded` with body and query string parameters?AWS API Gateway 可以支持带有正文和查询字符串参数的“application/x-www-form-urlencoded”吗?
【发布时间】:2020-03-24 05:29:26
【问题描述】:

当使用Content-Type: application/x-www-form-urlencoded 和正文中的其他参数发出POST 请求时,许多服务可以接受URL 中的查询字符串参数,但AWS API Gateway 似乎不能同时接受查询字符串参数。

当我使用 application/x-www-form-urlencoded 的 POST 映射模板和查询字符串 URL 参数(使用 Lambda 函数)调用 AWS API Gateway 时,我收到以下错误:

{
  "message":"When Content-Type:application/x-www-form-urlencoded,
    URL cannot include query-string parameters (after '?'):
    '/prod/webhook?inputType=wootric&outputType=glip&url=...'"
}

这是一个 cURL 示例:

curl -XPOST 'https://{myid}.execute-api.{myregion}.amazonaws.com/prod/webhook? \
inputType=wootric&outputType=glip&url=https://hooks.glip.com/webhook/ \
11112222-3333-4444-5555-666677778888' \
-d "@docs/handlers/wootric/event-example_response-created.txt" \
-H 'Content-Type: application/x-www-form-urlencoded' -v

具体目标是使用带有查询字符串参数的 URL 将 Wootric webhook 事件发布到 Lambda 函数。

您可以在这里获取代码:

https://github.com/grokify/chathooks

Wootric 事件正文文件在这里:

https://raw.githubusercontent.com/grokify/chathooks/master/docs/handlers/wootric/event-example_response-created.txt

GitHub 问题在这里:

https://github.com/grokify/chathooks/issues/15

错误信息似乎很明确,但我想问一下:

  1. 是否有解决方法来配置 API 网关以支持两者?
  2. AWS 不支持这一点是否有基于标准的原因,或者这只是一个设计决策/限制?

如果没有解决方案,除了部署像 Heroku 这样的托管服务器解决方案之外,还有没有好的轻量级解决方案。此外,其他云服务是否通过其 API 网关 + 云功能支持此功能,例如 Google?

一些显示支持两者的示例:

【问题讨论】:

  • 您是否以某种方式解决了这个问题?

标签: amazon-web-services aws-api-gateway query-string x-www-form-urlencoded


【解决方案1】:

是的,有一个解决方法,关键问题是设置将字符串转换为 json 的映射模板。非常详细的示例显示在 API Gateway any content type.

【讨论】:

  • 我需要支持application/x-www-form-urlencoded(带有正文参数)和查询字符串(url)参数,如上所述。链接中的答案仅包括正文参数,不包括查询字符串参数。 Postman 屏幕截图中没有显示查询字符串参数。我更新了问题以使其更清晰。
  • 上面添加的示例 cURL 带有指向 GitHub 存储库和 GitHub 问题的链接。我已经配置好了映射模板。
  • 此评论中的链接似乎已损坏 :(
【解决方案2】:

请将您的 HttpURLConnection 的请求属性设置为“Content-Type”、“application/json”,如下所示 connection.setRequestProperty("Content-Type", "application/json");

【讨论】:

    【解决方案3】:

    我遇到了类似的问题,第 3 方提供商使用网络挂钩。事实证明,我的提供商正在将 url 路径从 UPPERCASE 转换为 LOWERCASE。例如端点应该是apigateway.com/dev/0bscur3dpathRANDOM 而不是apigateway.com/dev/0bscur3dpathRANDOM。你明白了。

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 1970-01-01
      • 2019-05-24
      • 2018-12-27
      • 2018-05-12
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多