有没有办法更改Amazon API Gateway返回的http状态代码? [英] Is there a way to change the http status codes returned by Amazon API Gateway?

查看:145
本文介绍了有没有办法更改Amazon API Gateway返回的http状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我想针对无效参数返回特定的400错误,或者在lambda函数调用导致创建时返回201.

For instance if I want to return a specific 400 error for invalid parameters or perhaps a 201 when the lambda function call resulted in a create.

我想使用不同的http状态代码,但即使lambda函数返回错误,api网关也总是返回200状态代码.

I'd like to have different http status codes but it looks like api gateway always returns a 200 status code even if the lambda function is returning an error.

推荐答案

根据2016年9月20日更新

Amazon最终使用

Amazon finally made this easy using the Lambda Proxy integration. This allows your Lambda function to return proper HTTP codes and headers:

let response = {
    statusCode: '400',
    body: JSON.stringify({ error: 'you messed up!' }),
    headers: {
        'Content-Type': 'application/json',
    }
};

context.succeed(response);

在API网关中说再见请求/响应映射!

Say goodbye request/response mapping in the API Gateway!

选项2

使用 aws-serverless-express 将现有的Express应用程序与Lambda/API网关集成.

Integrate an existing Express app with Lambda/API Gateway using aws-serverless-express.

这篇关于有没有办法更改Amazon API Gateway返回的http状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆