这种情况下的 HTTP 状态代码(400 或 422)
HTTP status code for this case (400 or 422)
我有一些看起来像这样的方法
def some_method
if params["status"] == "good"
some action
render json: {message: "action success"}, status: 200
else
render json: {message: "The status is not good, try again later when the status is good"}, status: 200
end
end
你看什么时候状态不好,目前我返回200状态码,这种情况最好的状态码是多少。是 400 还是 422?
422 的描述说:
The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions.
因此,总而言之,这取决于您的用例详细信息。
加载64!
维基百科上提供了 HTTP Status codes 的列表,应该对您非常有帮助。如果这是对用户输入进行身份验证,那么正确的状态代码将是 401
.
当我构建 API 时,通常我使用 422 aka :unprocessable_entity 来表示验证错误。
在我看来,:unprocessable_entity 看起来更明确。
我有一些看起来像这样的方法
def some_method
if params["status"] == "good"
some action
render json: {message: "action success"}, status: 200
else
render json: {message: "The status is not good, try again later when the status is good"}, status: 200
end
end
你看什么时候状态不好,目前我返回200状态码,这种情况最好的状态码是多少。是 400 还是 422?
422 的描述说:
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions.
因此,总而言之,这取决于您的用例详细信息。
加载64!
维基百科上提供了 HTTP Status codes 的列表,应该对您非常有帮助。如果这是对用户输入进行身份验证,那么正确的状态代码将是 401
.
当我构建 API 时,通常我使用 422 aka :unprocessable_entity 来表示验证错误。
在我看来,:unprocessable_entity 看起来更明确。