带有Lambda代理的AWS API Gateway始终会产生base64字符串响应 [英] AWS API Gateway with Lambda proxy always produces base64 string response

查看:124
本文介绍了带有Lambda代理的AWS API Gateway始终会产生base64字符串响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​ Gateway Lambda代理集成,并尝试返回二进制application/protobuf响应.不管我做什么,响应主体始终是base64编码的字符串

I'm using API Gateway Lambda proxy integration and trying to return a binary application/protobuf response. No matter what I do, the response body is always a base64 encoded string

  • 我在APIG中将application/protobuf设置为binary media types
  • 我的客户端(javascript)在POST中发送以下标头: Accept: application/protobuf Content-Type: application/protobuf
  • 我的lambda响应content-type: application/protobuf,并将IsBase64Encoded Lambda响应正确设置为true
  • I have application/protobuf setup as a binary media types in APIG
  • My client (javascript) is sending following headers in the POST: Accept: application/protobuf Content-Type: application/protobuf
  • My lambda is responing with content-type: application/protobuf, and correctly setting the IsBase64Encoded Lambda response to true

如何使APIG对base64解码字符串?我发誓几个月前我第一次尝试这个功能时就可以使用.

How do you get APIG to base64 decode the string? I swear I had this working a few months ago when I 1st tried this.

注意:我也尝试过将*/*用作binary media types

Note: I've also tried */* as a binary media types

一些相关帖子以添加背景

Some related posts to add background:

  • https://github.com/twitchtv/twirp/issues/81
  • https://github.com/awslabs/aws-serverless-express/issues/39#issuecomment-276019222

更新:

结果是,只有将binary media type设置为*/*时,我才能使它工作.客户端Accept标头一旦设置为此就没有影响.

Turns out I can only get it working if binary media type is set to */*. The client Accept header has no impact once it is set to this.

使用*/*会带来很多不良影响,因为尝试对每个响应进行解码(即使IsBase64Encoded为false或未设置)

Many bad side effects of using */* because every response is attempted to get decoded (even when IsBase64Encoded is false or not set)

我认为它没有解码,因为Chrome网络检查工具将始终在Preview标签中将二进制数据显示为以base64编码的形式.您可以在Response选项卡中看到protobuf.

I thought it wasn't decoding because Chrome network inspect tools will always show binary data as base64 encoded in the Preview tab. You can see the protobuf in the Response tab.

推荐答案

问题是我在API Gateway之前使用CloudFront,并且没有将Accept标头传递给源(APIG).

The problem was I'm using CloudFront in front of API Gateway, and I was not passing the Accept header to the origin (APIG).

使用Lambda代理处理二进制文件的文档并不出色,因此这里是一个简短的摘要:

The docs on handling binary with Lambda proxy are not great, so here is a quick summary:

  • 您的客户必须发送第一种媒体类型匹配您在API网关中设置为binary media types的内容
  • 您的Lambda如果提供二进制媒体类型,则必须将IsBase64Encoded设置为true,并且body必须是base64编码的
  • Your client must send an Accept header who's 1st media type matches what you have set as a binary media types in API Gateway
  • Your Lambda, if serving a binary media type, must set IsBase64Encoded to true AND the body must be base64 encoded

如果客户端Accept标头与API Gateway的binary media types中的条目匹配,并且满足这些条件,则API Gateway将在发送响应到客户端之前进行转换(base64解码).

If the clients Accept header matches an entry in API Gateway's binary media types and these conditions are met, API Gateway will transform (base64 decode) before sending a response to the client.

此博客文章逐步指导您如何使其工作(不使用CloudFront ).

This blog post walks you through step-by-step on how to get it working (without CloudFront).

是一个完整的

This is a full blown aws-blueprint for getting a production grade ci/cd with CloudFront.

这篇关于带有Lambda代理的AWS API Gateway始终会产生base64字符串响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆