【问题标题】:AWS API Gateway Binary output without Accept header没有 Accept 标头的 AWS API Gateway 二进制输出
【发布时间】:2017-04-23 13:14:00
【问题描述】:

根据http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html

If contentHandling is not defined, and if the Content-Type header of the response and the Accept header of the original request match an entry of the binaryMediaTypes list, API Gateway passes through the body. This occurs when the Content-Type header and the Accept header are the same; otherwise, API Gateway converts the response body to the type specified in the Accept header.

我无法发出发送Accept 标头的原始请求。 如何让我的 AWS API Gateway 以二进制格式返回? (图像/JPEG)。因为我从 s3 发回的图像在最后阶段被转换为文本(因为原始请求缺少 Accept: image/jpeg 标头)。

【问题讨论】:

  • 亲爱的 Pakore,您应该接受 Kenneth Rory Dunn 的回答 -> 它有效

标签: http amazon-web-services header binary aws-api-gateway


【解决方案1】:

如果您想使用 lambda 代理集成来执行此操作,您可以将值 */* 传递给二进制媒体类型,然后它不会尝试匹配接受标头。

这是一篇更详细的文章https://www.sspilsbury.com/2018-03-04-getting-aws-lambda-and-api-gateway-to-support-binary-2018-edition/

【讨论】:

    【解决方案2】:

    将集成响应中的“内容处理”设置为“转换为二进制”。在集成响应中设置转换为二进制时,Content-Type API Gateway 响应标头为“*/*”,因此您需要在 Content-Type 的集成响应中创建标头映射,映射到 integration.response.header。内容类型。这将确保 API Gateway 响应包含与后端传递给 API Gateway 的响应相同的 Content-Type 值。

    为了清楚起见,这里是所需设置的摘要。

    集成响应:

    • 内容处理:转换为二进制
    • 标头映射:Content-Type:integration.response.header.Content-Type

    这消除了客户端接受请求标头的需要。

    【讨论】:

    • 您能否提供一个示例来说明如何在 CDK 中执行此操作?我正在努力让 PDF 显示在浏览器中,因为浏览器不发送 Accept: application/pdf 标头。我已经在 API 资源上设置了binaryMediaTypes,还在 lambda 集成上设置了 CONVERT_TO_BINARY。但我很难设置标题映射。
    • 如果您尝试在没有匹配的接受标头的情况下在 Lambda 中进行这项工作,则这将不适用于“Lambda 代理集成”。据我所知,使用 Lambda 代理时,您无法直接从 API 网关修改内容类型。改用“Lambda 非代理(自定义)”集成
    猜你喜欢
    • 2019-06-19
    • 2021-04-06
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多