首页 > 解决方案 > 未找到 stepfunction 时将状态码从 200 更改为 404

问题描述

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

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

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

你能帮我吗?

回复:

状态:200ok

预期的:

状态:404

谢谢,

哈里卡。

标签: amazon-web-servicesaws-api-gatewayaws-step-functions

解决方案


根据文档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因此,在使用状态机调用返回的响应(200)的情况下,non-existent它是正确的。但是,当API Gateway尝试调用non-existentstatemachine 时,它404​​从StartExecutionapi 调用中获得,它最终包装成正确的消息,而不是返回404http 响应。

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的确切信息,并且您必须通过提供OR a来选择。HTTP Status Code404Integration ResponseMethod ResponseExact HTTP Responce Code(400 -> Upstream response from the **StartExecution** API Call)Regex -> (4\{d}2) matching all the 4xx errors

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

  • ExecutionAlreadyExists -> 400
  • ExecutionLimitExceeded -> 400
  • InvalidArn -> 400
  • 无效的执行输入-> 400
  • 无效名称 -> 400
  • 状态机删除 -> 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'"
}

我认为这会产生误导。


推荐阅读