【问题标题】:AWS API Gateway, "HTTP status regex" in "Integration Response" cannot match any stringAWS API Gateway,“集成响应”中的“HTTP 状态正则表达式”无法匹配任何字符串
【发布时间】:2016-11-16 09:22:17
【问题描述】:

一些背景信息:我的设置是browser -> api gateway -> ec2 (using spring mvc)
我想返回 200 以外的 http 状态。在 google 中进行了大量搜索后,我知道我已经在 api gatway 上进行了一些配置。以下是我的配置:

  1. Method Response中,添加http状态400
  2. Integration Response -> HTTP status regex,添加.*httpStatus":400.*
  3. 在 ec2(使用 spring mvc)中,我总是返回 http 状态 400,并带有以下 json 作为响应体(用于测试)

    {
    "errorMessage": "{\"errorType\":\"Bad Request\",\"requestId\":\"12345\",\"httpStatus\":400,\"message\":\"你有错误\"}"
    }

但是,当我测试 api 时(使用 api 网关中的“测试”按钮),它仍然返回 http 状态 200。(我希望它应该是 400)
当我将正则表达式更改为 .*400.* 时,它会按预期返回 http 状态 400。
当我将正则表达式更改为 .*httpStatus.* 时,它返回 http 状态 200。(我希望它应该是 400)

为什么正则表达式 .*httpStatus":400.* 不能识别我的 json?即使我将正则表达式简化为.*httpStatus.*
但是正则表达式 .*400.* 可以识别 int 400 ...

PS:我永远不会忘记在 api 网关发生任何更改后部署 api

这是日志

Execution log for request test-request  
Wed Jul 13 15:38:22 UTC 2016 : Starting execution for request: test-invoke-request  
Wed Jul 13 15:38:22 UTC 2016 : HTTP Method: POST, Resource Path: /graphql  
Wed Jul 13 15:38:22 UTC 2016 : Method request path: {}  
Wed Jul 13 15:38:22 UTC 2016 : Method request query string: {}  
Wed Jul 13 15:38:22 UTC 2016 : Method request headers: {}  
Wed Jul 13 15:38:22 UTC 2016 : Method request body before transformations: null  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint request URI: http://awseb-e-e-AWSEBLoa-1OSQBKG0O3J75-968940376.ap-northeast-1.elb.amazonaws.com/graphql  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint request headers: {x-amzn-apigateway-api-id=kymhygbqwa, Accept=application/json, User-Agent=AmazonAPIGateway_kymhygbqwa}  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint request body after transformations: null  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint response body before transformations: {"errorMessage":"{\"errorType\":\"Bad Request\",\"requestId\":\"12345\",\"httpStatus\":400,\"message\":\"you got error\"}"}  
Wed Jul 13 15:38:22 UTC 2016 : Endpoint response headers: {date=Wed, 13 Jul 2016 15:38:22 GMT, server=Apache-Coyote/1.1, expires=0, transfer-encoding=chunked, access-control-allow-headers=Origin, X-Requested-With, Content-Type, Accept, x-frame-options=DENY, access-control-allow-methods=POST, GET, PUT, OPTIONS, DELETE, pragma=no-cache, access-control-allow-origin=*, access-control-max-age=3600, x-content-type-options=nosniff, x-xss-protection=1; mode=block, content-type=application/json;charset=UTF-8, connection=keep-alive, cache-control=no-cache, no-store, max-age=0, must-revalidate}  
Wed Jul 13 15:38:22 UTC 2016 : Method response body after transformations: {"errorMessage":"{\"errorType\":\"Bad Request\",\"requestId\":\"12345\",\"httpStatus\":400,\"message\":\"you got error\"}"}  
Wed Jul 13 15:38:22 UTC 2016 : Method response headers: {Content-Type=application/json}  
Wed Jul 13 15:38:22 UTC 2016 : Successfully completed execution  
Wed Jul 13 15:38:22 UTC 2016 : Method completed with status: 200  

【问题讨论】:

    标签: amazon-web-services aws-api-gateway


    【解决方案1】:

    你缺少一个关键元素:

    对于 HTTP 和 AWS 集成,错误模式与集成响应的 HTTP 状态代码相匹配。对于 Lambda“调用”集成,针对响应中“errorMessage”字段的错误模式。

    您需要使用 SpringMVC 端点的响应状态代码来控制 API Gateway 响应。

    作为参考,这块智慧被埋在第 5.b 节here

    另请参阅有关错误映射的一些更精细点的博客文章here

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题,我想你只需要稍微改变一下你的正则表达式。

      就我而言:

      拉姆达:

      // scores should be numeric
      if(isNaN(event.body.score) || isNaN(event.body.maxScore)){
        throw new Error('[400] Score and maxScore should be numbers! For example: 8 
        or 8.5');
      }
      

      集成响应: ^[400].*

      【讨论】:

        【解决方案3】:

        我有一个快速应用程序,并在它前面使用了带有 http 集成的 api 网关。要从我的应用程序返回状态代码而不是 200 OK ,我只需将应用程序的错误处理程序返回的 http 状态代码添加到集成响应部分的 http 状态正则表达式中,它就可以正常工作。确保您的应用程序正确处理错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-18
          • 2013-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-19
          相关资源
          最近更新 更多