【问题标题】:Create a custom success AWS API Gateway model创建自定义成功 AWS API Gateway 模型
【发布时间】:2020-03-22 03:27:47
【问题描述】:

我没有在 Stack Overflow 上找到任何东西,而且许多使用过 AWS 的人都知道,他们的文档可能很麻烦。如果我只需要指向一些文档,我将不胜感激。

我为客户构建了一个 API 端点。网关后面的一切工作正常,但我想为我的客户提供不同的消息以获得成功响应,而不是 200 状态代码,正文为:null

我已进入 API Gateway 中的“模型”菜单。我试图对错误响应进行逆向工程,但它似乎接受了一个对象,而且我的 200 秒仍然为 null。

错误模型架构,API Gateway 中的默认值:

{
  "$schema" : "http://json-schema.org/draft-04/schema#",
  "title" : "Error Schema",
  "type" : "object",
  "properties" : {
    "message" : { "type" : "string" }
  }
}

上面 JSON 中的这个 URL 指向一个我不知道如何使用的模式文件。

如果 API 密钥不正确,网关返回

{
   "message": "Forbidden"
}

如何让 API Gateway 在成功时返回以下内容?:

{
   "message": "Success"
}

而不是

null

【问题讨论】:

    标签: amazon-web-services aws-api-gateway api-gateway


    【解决方案1】:

    我的一个错误是我依赖 API Gateway 来给出响应,即使我将它用作 lambda 函数的网关。

    在 lambda 中:

    let fetch = require('node-fetch');
    
    exports.handler = async function(event, context) {
    
        event = JSON.stringify(event);
    
        await fetch(url, {method: 'POST', body: event});
    
        context.succeed({message: "Success"});
    
    };
    

    我必须让我的函数异步,并等待我的 fetch 响应。一旦我知道了,我就可以使用回调并返回消息。现在一切正常。我在邮递员中的回复是

    {
        "message": "Success"
    }
    

    您也可以为其他响应类型添加条件,或.then().catch() 来处理成功和失败。该解决方案不是要求 API 网关回答,而是要求您使用网关的端点。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-06
      • 2018-12-26
      • 2020-10-04
      • 2017-09-30
      • 2018-05-29
      • 1970-01-01
      • 2017-02-08
      • 2016-11-30
      相关资源
      最近更新 更多