可以通过添加Lambda函数在API Gateway中验证并填充Host头信息来解决该问题。下面是示例代码:
import json
def lambda_handler(event, context):
headers = event['headers']
host_header = headers.get('Host', None)
if host_header is None:
response = {
"statusCode": 400,
"body": json.dumps({"message":"Missing Host header"})
}
else:
response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : True,
"Access-Control-Allow-Methods" : "OPTIONS, GET",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Host": host_header
},
"body": json.dumps({"message":"Success"})
}
return response
这个Lambda函数会验证是否存在Host头信息,如果不存在,则返回400错误。如果存在,则填充Host头信息并返回200成功。在API Gateway中,将Lambda函数与APIGateway API集成,将需要进行Host头验证的所有资源路径设置为使用该Lambda函数作为代理,即可解决该问题。