You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda中307重定向POST请求时如何传递自定义请求体?

解决AWS Lambda POST重定向时自定义请求体传递问题

你需要从AWS Lambda重定向到外部POST端点,使用自定义请求体而非原请求体,当前用307重定向能触发目标端点,但Spring Boot日志报请求体缺失的500错误;301/302仅适用于GET请求,POST场景需传递敏感数据无法使用这类重定向。

Lambda代码示例

import json

def lambda_handler(event, context):
    
    # Create Dictionary
    value = {
        "language": "python33",
        "greetings" : "Greetings... Hope you have been redirected"
    }
 
    # Dictionary to JSON Object using dumps() method
    # Return JSON Object
    return {
    "headers": {"Location": "http://localhost:8080/urlcallback",'Content-Type':'application/json' ,},
    'statusCode': 307,
    'body':  json.dumps(value)
    }

Spring Boot错误日志

{
    "timestamp": 1671820676750,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "Required request body is missing: public java.util.Map<java.lang.String, java.lang.String> my.sample.service.controller.PingController.urlcallback(java.lang.String)",
    "path": "/urlcallback"
}

问题原因

307重定向的规则是让客户端重新发送原始请求的方法和请求体,而非使用Lambda返回的body内容。所以你自定义的请求体不会被客户端转发到目标端点,导致Spring Boot因缺少必填请求体报错。

解决方案

不要依赖重定向,直接在Lambda内部作为代理发起POST请求到目标端点,传递自定义请求体,再将目标端点的响应返回给客户端。

修改后的Lambda代码(使用requests库)

import json
import requests

def lambda_handler(event, context):
    # 自定义请求体
    payload = {
        "language": "python33",
        "greetings": "Greetings... Hope you have been redirected"
    }
    
    try:
        # 直接调用目标POST接口
        target_response = requests.post(
            url="http://localhost:8080/urlcallback",
            json=payload,
            headers={"Content-Type": "application/json"}
        )
        
        # 将目标接口的响应返回给客户端
        return {
            "statusCode": target_response.status_code,
            "headers": dict(target_response.headers),
            "body": target_response.text
        }
    except Exception as e:
        # 处理请求异常
        return {
            "statusCode": 500,
            "body": json.dumps({"error_msg": str(e)})
        }

无第三方依赖实现(使用内置urllib)

import json
from urllib.request import Request, urlopen

def lambda_handler(event, context):
    payload = {
        "language": "python33",
        "greetings": "Greetings... Hope you have been redirected"
    }
    payload_json = json.dumps(payload).encode('utf-8')
    
    req = Request(
        url="http://localhost:8080/urlcallback",
        data=payload_json,
        headers={"Content-Type": "application/json"},
        method='POST'
    )
    
    try:
        with urlopen(req) as response:
            response_body = response.read().decode('utf-8')
            return {
                "statusCode": response.getcode(),
                "headers": dict(response.info()),
                "body": response_body
            }
    except Exception as e:
        return {
            "statusCode": 500,
            "body": json.dumps({"error_msg": str(e)})
        }

注意事项

  • 使用requests库时,部署Lambda需将该库打包进部署包,或创建包含requests的Lambda层避免依赖缺失。
  • 代理方式不会暴露目标端点给客户端,敏感数据仅在Lambda与目标服务之间传递,安全性更高。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 06:05:14