You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

AWS API Gateway Lambda代理集成无法获取自定义Header问题咨询

Fixing Missing Custom Headers & Content-Type in API Gateway Lambda Proxy Integration

Hey there! I totally get how frustrating this can be—especially when you’re deep into building multipart request handling and hit this roadblock. Let’s break down exactly how to get those headers showing up in your Lambda function.

1. Double-Check API Gateway Integration Request Settings

By default, API Gateway’s Lambda Proxy integration doesn’t pass all request headers to your Lambda—only a subset (like Authorization which you’re seeing). You need to explicitly map the headers you want:

  • Go to your API in the AWS Console, navigate to the specific method (e.g., POST)
  • Click Integration Request
  • Scroll down to HTTP Headers and click Add header
    • For Content-Type: Set the Name to Content-Type, and the Mapping value to method.request.header.Content-Type
    • For custom headers (e.g., X-My-Custom-Header): Repeat the same, using method.request.header.X-My-Custom-Header as the mapping value
  • Don’t forget to Deploy your API after making these changes—this is easy to miss!

2. Watch for Header Case Sensitivity in Lambda

Here’s a common gotcha: API Gateway converts all header names to lowercase when passing them to Lambda via proxy integration. So even if you send Content-Type from your client, it’ll show up as content-type in the event.headers object.

For example, in a Python Lambda function, you’d access it like this:

import json

def lambda_handler(event, context):
    # Grab lowercase versions of the headers
    content_type = event.get("headers", {}).get("content-type")
    custom_header = event.get("headers", {}).get("x-my-custom-header")

    # Your multipart processing logic here...

    return {
        "statusCode": 200,
        "body": json.dumps({
            "received_content_type": content_type,
            "received_custom_header": custom_header
        })
    }

3. Special Considerations for Multipart Requests

If you’re dealing with multipart/form-data, there’s an extra thing to check:

  • Ensure API Gateway isn’t automatically parsing the request body. When using Lambda Proxy integration, this should be off by default, but if you’ve set up a request validator or body mapping template, double-check that it’s not stripping headers or modifying the request.
  • Make sure your client is sending the Content-Type header correctly, including the boundary parameter (e.g., Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW). Without the boundary, API Gateway might not process the request properly, leading to missing headers.

Quick Test Tip

Before diving deeper, test with a simple curl command to confirm headers are being sent:

curl -X POST https://your-api-id.execute-api.region.amazonaws.com/stage/your-endpoint \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: multipart/form-data; boundary=test-boundary" \
  -H "X-My-Custom-Header: hello-world" \
  -F "file=@test-file.jpg"

This will help you rule out client-side issues and confirm the problem is in API Gateway/Lambda configuration.

内容的提问来源于stack exchange,提问作者Matt Clevenger

火山引擎 最新活动