有没有办法来改变C $ CS亚马逊API网关返回HTTP状态$ [英] is there a way to change the http status codes amazon api gateway returns

查看:215
本文介绍了有没有办法来改变C $ CS亚马逊API网关返回HTTP状态$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举例来说,如果我想返回特定的400错误无效的参数或者一个201的时候lambda函数调用导致了创建。

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状态codeS,但它看起来像API网关总是返回200状态code,即使在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.

推荐答案

下面是返回自定义HTTP状态codeS和一个自定义的的errorMessage 最快的方式:

Here's the fastest way to return custom HTTP Status Codes and a custom errorMessage:

在API网关仪表盘,请执行以下操作:

In the API Gateway dashboard, do the following:

  1. 方法的为您的资源的,点击的方法响应
  2. HTTP状态的表,点击的添加响应的添加在每个HTTP状态code,你想使用。
  3. 方法的为您的资源的,点击的集成响应
  4. 添加的集成响应的每个HTTP状态codeS前面创建的。确保的输入直通的检查。使用的拉姆达错误的正则表达式的识别哪些状态,当你从你的lambda函数返回一个错误信息code应该使用。例如:

  1. In the method for your resource, click on method response
  2. In the HTTP Status table, click add response and add in each HTTP Status Code you would like to use.
  3. In the method for your resource, click on integration response
  4. Add an integration response for each of the HTTP Status Codes you created earlier. Make sure input passthrough is checked. Use lambda error regex to identify which status code should be used when you return an error message from your lambda function. For example:

// Return An Error Message String In Your Lambda Function

return context.fail('Bad Request: You submitted invalid input');

// Here is what a Lambda Error Regex should look like.
// Be sure to include the period and the asterisk so any text
// after your regex is mapped to that specific HTTP Status Code

Bad Request: .*

  • 您的API网关路由应该返回这样的:

  • Your API Gateway route should return this:

    HTTP Status Code: 400
    JSON Error Response: 
        {
            errorMessage: "Bad Request: You submitted invalid input"
        }
    

  • 我看不出有什么办法复制这些设置,并重新将其用于不同的方法,所以我们有很多烦人的冗余手动输入的事!

  • I see no way to copy these settings and re-use it for different methods, so we have much annoying redundant manual inputting to do!

    我的积分回应是这样的:

    My Integration Responses look like this:

    这篇关于有没有办法来改变C $ CS亚马逊API网关返回HTTP状态$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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