結合API Gateway和Lambda實現登陸時的重定向和表單提交請求(Python3實現)

1. 建立Lambda函數,代碼以下:app

 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函數。url

 

3.修改API響應參數,刪除200響應,添加302響應,添加響應標頭 Locationspa

 

 

 

 方法響應修改操做完,集成響應配置時會正常出現302和標頭:3d

 

添加標頭內容:integration.response.body.locationcode

 

 1選擇響應,2處會同步出現響應的302操做,部署orm

訪問這個API就能夠正常重定向了:blog

4.若是訪問連接是表單提交的話,須要添加集成請求的映射模板,部署

修改API集成請求參數:input

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

模板內容:

{
    "body": "$input.body" }
相關文章
相關標籤/搜索