简体   繁体   English

AWS Application Load Balancer 无法处理内容编码为 gzip 且内容类型为 application/json 的请求正文

[英]AWS Application Load Balancer fails to process request body for content encoding gzip and content type application/json

I am trying to send a gzipped json as POST request body to AWS Application Load Balancer, which calls AWS Lambda.我正在尝试将压缩后的 json 作为 POST 请求正文发送到调用 AWS Lambda 的 AWS Application Load Balancer。

When I set the content type request header as application/json, I get 502 Bad Gateway error as response and AWS Lambda does not get invoked.当我将内容类型请求 header 设置为 application/json 时,我收到502 Bad Gateway错误作为响应,并且 AWS Lambda 没有被调用。

I am using following curl command.我正在使用以下 curl 命令。

curl -v -s --data-binary @samples/small-batch.json.gz -H "Content-Encoding: gzip" -H "Content-Type: application/json" -X POST https://sub.domain.com/batch

Am I sending invalid request headers?我是否发送了无效的请求标头?

My AWS Lambda code:我的 AWS Lambda 代码:

import json

def lambda_handler(event, context):
    print("event = ", event)
    return {
            'statusCode': 200,
            'body': json.dumps({ 'success': True }),
            'headers': {
                'Content-Type': 'application/json'
            }
        }

Update更新

If I place request with empty content type, then Lambda gets called successfully.如果我发出内容类型为空的请求,则会成功调用 Lambda。

curl -v --data-binary @samples/small-batch.json.gz -H "Content-Type: " -H "Content-encoding: gzip" -X POST https://sub.domain.com/batch

If I make request with application/gzip content type then Lambda gets called successfully.如果我使用application/gzip内容类型发出请求,则会成功调用 Lambda。

curl -v --data-binary @samples/small-batch.json.gz -H "Content-Type: application/gzip" -H "Content-encoding: gzip" -X POST https://sub.domain.com/batch

The 502 error is occurred only when I request with Content Encoding as gzip and Content Type as application/json .只有当我请求内容编码为gzip和内容类型为application/json时,才会发生 502 错误。 But as per my understanding these are valid headers.但据我了解,这些是有效的标头。

Update 2更新 2

From the documentation I found at https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html从我在https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html找到的文档中

If the content type is one of the following types, the load balancer sends the body to the Lambda function as is and sets isBase64Encoded to false: text/*, application/json, application/javascript, and application/xml.如果内容类型是以下类型之一,负载均衡器将正文原样发送到 Lambda function 并将 isBase64Encoded 设置为 false:text/*、application/json、application/javascript 和 application/xml。 For all other types, the load balancer Base64 encodes the body and sets isBase64Encoded to true.对于所有其他类型,负载均衡器 Base64 对主体进行编码并将 isBase64Encoded 设置为 true。

I think because of this, header Content-Encoding: gzip can not be coupled with header Content-Type: application/json .我认为因此, header Content-Encoding: gzip不能与 header Content-Type: application/json耦合。 I think something is going wrong in ALB while calling Lambda.我认为在调用 Lambda 时 ALB 出现问题。

It's probably not a problem with header Content-Encoding: gzip & Content-Type: application/json but it could be a problem with the binary data size (which could be more than 1 MB) that you are sending.这可能不是 header Content-Encoding: gzip & Content-Type: application/json的问题,但它可能是您发送的二进制数据大小(可能超过 1 MB)的问题。

According to AWS documenation, Lambda has limitation in request and response payload while using with ALB.根据 AWS 文档,Lambda 在与 ALB 一起使用时在请求和响应负载方面存在限制。

  • The maximum size of the request body that you can send to a Lambda function is 1 MB.您可以发送到 Lambda function 的请求正文的最大大小为 1 MB。

  • The maximum size of the response JSON that the Lambda function can send is 1 MB. Lambda function 可以发送的响应 JSON 的最大大小为 1 MB。

Please refere: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html请参考: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过AWS Application Load Balancer提供Django内容 - Serving Django content through AWS Application Load Balancer 在AWS中使用Application Load Balancer(ALB)时出现混合内容问题 - Mixed content issue in using Application Load Balancer (ALB) in AWS AWS-具有Content-Type的Api Gateway POST正文:text / plain代替json / application to lambda - AWS - Api Gateway POST body with Content-Type: text/plain instead json/application to lambda AWS Application Load Balancer- - AWS Application Load Balancer - AWS Application Load Balancer 无法通过 Apache/MySQL DB 处理登录表单请求 - AWS Application Load Balancer unable to process login form request via Apache/MySQL DB AWS 应用程序负载均衡器 (ALB) 基于主体的路由 - AWS Application Load Balancer (ALB) Body Based Routing 带有经典负载均衡器的AWS Application负载均衡器 - AWS Application load balancer with classic load balancer Eureka服务器运行状况检查在AWS Application Load Balancer中失败 - Eureka server health check fails in AWS Application Load Balancer AWS Application Load Balancer无法正常工作 - AWS Application Load Balancer not working AWS 上用于微服务的应用程序负载均衡器 - Application Load Balancer on AWS for microservices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM