结合API Gateway和Lambda实现登录时的重定向和表单提交请求(Python3实现)

1. 创建Lambda函数,代码如下:

 1 from urllib import parse
 2 
 3 def lambda_handler(event, context):
 4     body = event['body']
 5     print("-----------body:", body)
 6     request_data = {}
 7     for key_value in body.split('&'):
 8         key, value = key_value.split('=', 1)
 9         request_data[parse.unquote(key)] = parse.unquote(value)
10     """
11         response = your function with return the url to redirect 
12     """
13     response = "https://www.example.com/"
14     return  {
15         "location" : response
16     }

2.创建API

创建完后选择第二步,集成请求,选择你要集成的区域和Lambda函数。

3.修改API响应参数,删除200响应,添加302响应,添加响应标头 Location

 

 方法响应修改操作完,集成响应配置时会正常出现302和标头:

 

添加标头内容:integration.response.body.location

 1选择响应,2处会同步出现响应的302操作,部署

访问这个API就可以正常重定向了:

4.如果访问链接是表单提交的话,需要添加集成请求的映射模板,

修改API集成请求参数:

Content-Type:application/x-www-form-urlencoded

模板内容:

{
    "body": "$input.body"
}
原文地址:https://www.cnblogs.com/sen-c7/p/10158491.html