【问题标题】:To change the status code from 200 to 404 when stepfunction not found未找到 stepfunction 时将状态码从 200 更改为 404
【发布时间】:2021-01-15 13:32:39
【问题描述】:

我正在尝试从 API Gateway 执行 AWS step 功能,它按预期工作。

每当我传递输入时, statemachinearn(stepfunction name to execute) 都会触发 step 函数。

但是它仍然返回状态码200,每当它无法找到stepfunction时,如果apigateway没有找到那个stepfunction,我想返回状态码404。

你能帮我解决这个问题吗

回复:

状态:200ok

预期:

状态:404

谢谢,

哈里卡。

【问题讨论】:

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


    【解决方案1】:

    根据文档StartExecution API 调用,对于不存在的状态机返回400 Bad Request,这与RESTful API 标准是正确的。

    StateMachineDoesNotExist

    指定的状态机不存在。

    HTTP 状态码:400

    从 RESTful API 的角度来看,端点 /execution/(我在 API Gateway 中为集成设置创建)是一种资源,无论它接受 GET 或 POST 或其他什么。 404 仅适用于资源 /execution/ 本身不存在时。如果/execution/ 端点存在,但调用失败(无论什么原因),response status code must be something other than 404

    因此,对于使用non-existent 状态机的POST 调用返回的响应(200)是正确的。但是当API Gateway 尝试调用non-existent 状态机时,它从StartExecution api 调用中得到404,它最终包装成正确的消息而不是返回404 http 响应。

    curl -s -X POST -d '{"input": "{}","name": "MyExecution17","stateMachineArn": "arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine"}'  https://123456asdasdas.execute-api.eu-central-1.amazonaws.com/v1/execution|jq .
    
    {
      "__type": "com.amazonaws.swf.service.v2.model#StateMachineDoesNotExist",
      "message": "State Machine Does Not Exist: 'arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine1'"
    }
    

    假设您创建另一个MethodResponse,您可以在其中提供您想要返回的确切HTTP Status Code 404,然后您执行Integration Response,您必须选择Method Response,方法是提供Exact HTTP Responce Code(400 -> Upstream response from the **StartExecution** API Call)Regex -> (4\{d}2) matching all the 4xx errors

    在这种情况下,您将为上游错误4xx StartExecution Errors 的所有响应提供404

    • ExecutionAlreadyExists -> 400
    • ExecutionLimitExceeded -> 400
    • InvalidArn -> 400
    • InvalidExecutionInput -> 400
    • 无效名称 -> 400
    • StateMachineDeleting -> 400
    • StateMachineDoesNotExist -> 400

    不存在的状态机:

    curl -s -X POST -d '{"input": "{}","name": "MyExecution17","stateMachineArn": "arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine1"}'  https://123456asdasdas.execute-api.eu-central-1.amazonaws.com/v1/execution|jq .
    
    
    < HTTP/2 404
    < date: Sat, 30 Jan 2021 14:12:16 GMT
    < content-type: application/json
    ...
    
    
    {
      "__type": "com.amazonaws.swf.service.v2.model#StateMachineDoesNotExist",
      "message": "State Machine Does Not Exist: 'arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine1'"
    }
    
    

    执行已经存在

    curl -s -X POST -d '{"input": "{}","name": "MyExecution17","stateMachineArn": "arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine"}'  https://123456asdasdas.execute-api.eu-central-1.amazonaws.com/v1/execution|jq .
    
    * We are completely uploaded and fine
    < HTTP/2 404
    < date: Sat, 30 Jan 2021 14:28:27 GMT
    < content-type: application/json
    
    {
      "__type": "com.amazonaws.swf.service.v2.model#ExecutionAlreadyExists",
      "message": "Execution Already Exists: 'arn:aws:states:eu-central-1:1234567890:execution:mystatemachine:MyExecution17'"
    }
    

    我认为这会产生误导。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多