如何在AWS api网关中正确使用集成响应映射以返回不同的http代码 [英] how to correctly use integration response mapping in aws api gateway to return different http codes
问题描述
我似乎无法使用Amazon api网关为错误设置集成响应
I can't seem to set my integration response for errors using the amazon api gateway
我添加了一个集成响应,但是它不返回400错误,而是继续返回200响应,
I added an integration response but it does not return the 400 error, instead it continues to return 200 response with
{
"errorMessage": "foose",
"errorType": "Error",
"stackTrace": [
"exports.handler (/var/task/index.js:11:19)"
]
}
推荐答案
如果您使用的是Java,则需要抛出异常.我犯了尝试返回错误信息的错误. Lambda错误正则表达式会解析Exception消息,因此,如果抛出此消息:
If you are using the Java, you need to throw an Exception. I made the mistake of trying to return the error information. The Lambda Error Regex parses the Exception message so if you throw this:
throw new Exception("Failed: Something bad happened!");
,然后将foo.*
替换为Failed: .*
,它将使用400状态代码.
and replace your foo.*
with Failed: .*
it will use the 400 status code.
如果您使用的是NodeJS,则可以使用context.fail('Failed: Something bad happened!');
获得相同的结果
If you are using NodeJS, you can use context.fail('Failed: Something bad happened!');
to get the same result
这篇关于如何在AWS api网关中正确使用集成响应映射以返回不同的http代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!