在API Gateway中启用集成响应,并在响应模板中添加Set-Cookie头部。以下是一个Lambda函数示例:
import json
def lambda_handler(event, context):
response = {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!'),
'headers': {
'Set-Cookie': 'cookie=value; Path=/; HttpOnly',
'Content-Type': 'application/json'
}
}
return response
在API网关集成响应中,选择“添加响应模板”,选择“application/json”并添加以下内容:
#set($inputRoot = $input.path('$'))
$input.json('$.headers').forEach($header => header($header.name, $header.value))
$input.json('$.cookies').forEach($cookie => header('Set-Cookie', $cookie))
$input.json('$.statusCode') $input.json('$.body')
然后在API Gateway中将同源策略设置为“false”并启用“Use Lambda Proxy Integration”选项。这样设置后,Set-Cookie头部将被正确地返回给客户端。