微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到 stepfunction 时将状态代码从 200 更改为 404

如何解决找不到 stepfunction 时将状态代码从 200 更改为 404

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

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

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

你能帮我吗

回复

状态:200ok

预期:

状态:404

谢谢,

哈里卡。

解决方法

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

状态机不存在

指定的状态机不存在。

HTTP 状态码:400

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

因此,对于带有 POST 状态机的 non-existent 调用返回的响应 (200) 是正确的。但是,当 API Gateway 尝试调用 non-existent 状态机时,它从 404 api 调用中获得了 StartExecution,最终将其包装成正确的消息而不是返回 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 ResponseMethod Response 来选择 Exact HTTP Responce Code(400 -> Upstream response from the **StartExecution** API Call)

在这种情况下,您将为上游错误 Regex -> (4\{d}2) matching all the 4xx errors StartExecution Errors

的所有响应提供 404
  • ExecutionAlreadyExists -> 400
  • ExecutionLimitExceeded -> 400
  • InvalidArn -> 400
  • 无效执行输入 -> 400
  • 无效名称 -> 400
  • 状态机删除 -> 400
  • StateMachineDoesNotExist -> 400

不存在的状态机:

4xx
curl -s -X POST -d '{"input": "{}","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": "{}","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

我认为这会产生误导。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。