有没有办法更改 Amazon API Gateway 返回的 http 状态代码? [英] Is there a way to change the http status codes returned by Amazon API Gateway?
问题描述
例如,如果我想为无效参数返回特定的 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 状态代码,但看起来 api 网关总是返回 200 状态代码,即使 lambda 函数返回错误.
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.
推荐答案
每 20-9-2016 更新
亚马逊最终使用 Lambda 代理集成.这允许您的 Lambda 函数返回正确的 HTTP 代码和标头:
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屋!