使用响应302从aws api网关重定向

时间:2016-06-24 08:54:49

标签: redirect aws-api-gateway

我正在尝试在AWS API网关中进行重定向工作。我将方法响应配置为在头部和Integration Response中包含Location,我将参数设置为:Location = integration.response.body.location。但是,当我测试API时,它只是向我显示API页面上的位置文本,而不是将我重定向到该位置。有没有人遇到过这个?

显示的字符串实际上是正确的位置,但API不会将我重定向到该URL。

8 个答案:

答案 0 :(得分:0)

您确定API端点返回302吗?

确保您没有为您的方法定义的302以外的任何2xx或3xx响应代码。你只能有一个成功的"响应代码映射。 2xx和3xx代码都被视为成功代码。

答案 1 :(得分:0)

好的,我发现了问题。我将存储位置的变量定义为字符串而不是对象,这就是为什么网页将其显示为文本。它现在重定向到页面。

答案 2 :(得分:0)

首先,您要为"位置"创建方法响应。头。然后,您要从Lambda的响应中映射位置标头值。在Node.js运行时,您可以将其映射到integration.response.body.errorType。通过这种方式,您可以通过以下方式重定向:

// Returns 302 or 301
var err = new Error("HandlerDemo.ResponseFound Redirection: Resource found elsewhere");
err.name = "http://a-different-uri";
context.done(err, {});

有关完整的文章,请参阅:https://aws.amazon.com/blogs/compute/redirection-in-a-serverless-api-with-aws-lambda-and-amazon-api-gateway/

答案 3 :(得分:0)

在此之后我也遇到了这个问题:https://aws.amazon.com/blogs/compute/redirection-in-a-serverless-api-with-aws-lambda-and-amazon-api-gateway/

还继续获得带有文本的200响应(尽管在API网关上删除了200)。

我们的团队只需要在API Gateway上重新部署更改,以使其正常工作! (Lambda只需要保存,实际上需要重新部署API网关)

答案 4 :(得分:0)

现在是2018年。通过API网关进行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

示例取自https://blog.rowanudell.com/redirects-in-serverless/

答案 5 :(得分:0)

使用AWS Lambda无需使用API​​网关 即可重定向到硬编码URL。这是导出的OpenAPI 3.0定义,以了解如何实现此目标:

      DefinitionBody:
        openapi: "3.0.0"
        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"

答案 6 :(得分:0)

这是我在Lambda中实际使用的内容。我让API很好。 API收集发送给它的数据:表单数据。这是非常简单的代码,您可以做很多事情。

我以lambda处理表单数据:提取变量,将其重新验证,将其保存到dynamoDB中,然后发出一封电子邮件,要求新注册以验证其满足GDPR的意图,并确保我已从某人那里获得了真实的电子邮件地址有能力并有兴趣进行互动,然后提供屏幕响应。

屏幕上的回复是重定向到特定的感谢页面,解释他们需要检查电子邮件并单击链接。

在该示例中,您需要做的就是将Amazon.com切换为您的目标网页。

通过使用变量:redirectURL,我可以改变响应,并在不同的网站页面上使用API​​和Lambda处理各种不同的形式。如果需要,我可以将重定向设置为表单本身中的隐藏表单字段。

    let redirectURL= 'https://amazon.com/';
    var response = {
        "statusCode": 302,
        "headers": {
            Location: redirectURL,
        }
    };
    callback(null, response);

答案 7 :(得分:0)

要使用lambda函数和api网关将某些结果重定向到其他uri,您必须使用

var err = new Error("HandlerDemo.ResponseFound Redirection: Resource found elsewhere"); err.name = result; context.done(err, {}); 在lambda函数中,因此上下文将转发错误 并在api网关上 方法响应: 将HTTP状态添加为302 302的响应标题 添加位置

For Mapping Integration Response Header Mapping Location integration.response.body.errorType

这将起作用

此网址https://aws.amazon.com/blogs/compute/redirection-in-a-serverless-api-with-aws-lambda-and-amazon-api-gateway/的信用

相关问题