首页 文章

将API部署到Amazon API Gateway

提问于
浏览
4

在Amazon API Gateway中,我创建了一个简单的API,其中包含一个名为 demo 的资源和一个与之对应的 POST 方法:

enter image description here

现在,我希望我的 endpoints 接受 any Content-Type的POST请求,因此不一定是application / json,而是plain / text . 然后我想获取请求的主体并将其包装在JSON对象中并将其发送到Amazon Lambda函数(Lambda函数只能接受JSON对象作为参数) .

为此,我编辑了与我的方法相对应的集成请求,以使用自定义模板映射:

enter image description here

我使用了亚马逊文档中的参考资料,可以在这里找到:http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

我的Lambda函数如下所示:

exports.handler = function(event, context) {
    context.succeed(event);
};

测试时,无论发送什么,我都会得到预期的输出:

enter image description here

但是,当我部署时,转换不再起作用,它需要JSON

  • 发送任何东西大喊这个:

enter image description here

  • 发送JSON会产生以下结果:

enter image description here

这个过程的任何部分是否执行错误?我在部署时遗漏了什么?对我来说,它看起来像一个非常烦人的亚马逊bug,任何人都可以确认吗?

2 回答

  • 1

    使用内容类型“application / json” .

    https://gist.github.com/maruks/e036168263cd412146e6

  • 3

    您是否尝试在curl请求中设置Accept和Content-Type标头?下面我假设您发送“text / plain”并且可以在响应中接受json

    -H "Content-Type: text/plain" -H "Accept: application/json"
    

相关问题