leo*_*o c 5 redirect aws-api-gateway
我正在尝试在AWS API网关中进行重定向工作.我将方法响应配置为在头部和Integration Response中包含Location,我将参数设置为:Location = integration.response.body.location.但是,当我测试API时,它只是向我显示API页面上的位置文本,而不是将我重定向到该位置.有没有人遇到过这个?
显示的字符串实际上是正确的位置,但API不会将我重定向到该URL.
小智 9
可以在不使用 AWS Lambda 的情况下使用API Gateway 重定向到硬编码的 URL 。这是关于如何实现的导出的 OpenAPI 3.0 定义:
openapi: "3.0.0"
info:
title: 'TheApiName'
version: ''
paths:
"/":
get:
responses:
"302":
description: "302 response"
headers:
Location:
schema:
type: "string"
content: {}
x-amazon-apigateway-integration:
responses:
default:
statusCode: "302"
responseParameters:
method.response.header.Location: "'https://github.com/lukedemi/adventbot'"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
Run Code Online (Sandbox Code Playgroud)
请注意,当您导入此 API 模板时,您仍然需要“部署”它,这样做时您需要创建一个“阶段”,然后将 API:Stage 与自定义域相关联(首次创建时需要 ACM 证书) API 网关中的自定义域),然后您只能将此单个 API 部署到域,因为您需要使用空的基本路径(解析为/),并且/anything由于 API 网关的工作方式,该映射不允许任何其他路由。
使用 API Gateway 进行 Lambda 重定向有一种更简单的方法:
module.exports.handler = (event, context, callback) => Promise.resolve(event)
// .then(doStuffWithTheEventObject)
.then(() => callback(null, {
statusCode: 302,
headers: {
Location: 'https://gohere.com',
},
})) // Success!
.catch(callback); // Fail function with error
Run Code Online (Sandbox Code Playgroud)
这个例子来自https://blog.rowanudell.com/redirects-in-serverless/
您确定 API 端点返回 302 吗?
确保除了为您的方法定义的 302 之外,没有任何 2xx 或 3xx 响应代码。您只能拥有一个“成功”响应代码映射。2xx 和 3xx 代码都被视为成功代码。