Rest API状态代码

时间:2014-10-13 08:58:14

标签: node.js rest http

我有一个API来检查用户是否存在。

POST /apis/user
[username=user]

如果用户已经存在,则API需要返回一个有意义的状态,表明用户已经存在。

如果用户存在,我将返回200OK。

如果用户不存在,返回状态应为什么 A> 404 Not Found
OR
B个200OK(因为无论如何都可以访问API),并显示消息{user already exists}

3 个答案:

答案 0 :(得分:0)

这是我在使用RESTful API时保持方便的文本文档。在某些方面,这是一个猜谜游戏,但主要是保持您的API一致。你会发现一些关于何时使用401 vs 403(以及其他)的讨论,或者可以应用于特定错误的多种类型。只要保持一致,您的API和错误消息对于使用它的人以及您在调试时对您有意义。

var STATUS_CODES = exports.STATUS_CODES = {
  100 : 'Continue',
  101 : 'Switching Protocols',
  102 : 'Processing',                 // RFC 2518, obsoleted by RFC 4918
  200 : 'OK',
  201 : 'Created',
  202 : 'Accepted',
  203 : 'Non-Authoritative Information',
  204 : 'No Content',
  205 : 'Reset Content',
  206 : 'Partial Content',
  207 : 'Multi-Status',               // RFC 4918
  300 : 'Multiple Choices',
  301 : 'Moved Permanently',
  302 : 'Moved Temporarily',
  303 : 'See Other',
  304 : 'Not Modified',
  305 : 'Use Proxy',
  307 : 'Temporary Redirect',
  400 : 'Bad Request',
  401 : 'Unauthorized',
  402 : 'Payment Required',
  403 : 'Forbidden',
  404 : 'Not Found',
  405 : 'Method Not Allowed',
  406 : 'Not Acceptable',
  407 : 'Proxy Authentication Required',
  408 : 'Request Time-out',
  409 : 'Conflict',
  410 : 'Gone',
  411 : 'Length Required',
  412 : 'Precondition Failed',
  413 : 'Request Entity Too Large',
  414 : 'Request-URI Too Large',
  415 : 'Unsupported Media Type',
  416 : 'Requested Range Not Satisfiable',
  417 : 'Expectation Failed',
  418 : 'I\'m a teapot',              // RFC 2324
  422 : 'Unprocessable Entity',       // RFC 4918
  423 : 'Locked',                     // RFC 4918
  424 : 'Failed Dependency',          // RFC 4918
  425 : 'Unordered Collection',       // RFC 4918
  426 : 'Upgrade Required',           // RFC 2817
  500 : 'Internal Server Error',
  501 : 'Not Implemented',
  502 : 'Bad Gateway',
  503 : 'Service Unavailable',
  504 : 'Gateway Time-out',
  505 : 'HTTP Version not supported',
  506 : 'Variant Also Negotiates',    // RFC 2295
  507 : 'Insufficient Storage',       // RFC 4918
  509 : 'Bandwidth Limit Exceeded',
  510 : 'Not Extended'                // RFC 2774
};

Nodejs Restify

BadDigestError: 400,
BadMethodError: 405,
InternalError: 500, // Don't have InternalErrorError
InvalidArgumentError: 409,
InvalidContentError: 400,
InvalidCredentialsError: 401,
InvalidHeaderError: 400,
InvalidVersionError: 400,
MissingParameterError: 409,
NotAuthorizedError: 403,
PreconditionFailedError: 412,
RequestExpiredError: 400,
RequestThrottledError: 429,
ResourceNotFoundError: 404,
WrongAcceptError: 406

答案 1 :(得分:0)

如果您查看normative definition of 4xx HTTP error status(由客户端引起的错误),您将看到唯一合适的定义是:

  

服务器理解请求,但拒绝履行请求。授权无效,请求不应重复。

此定义对应403 Forbidden

请注意:

  

如果请求方法不是HEAD并且服务器希望公开为什么请求未被满足,则它应该描述实体中拒绝的原因。

因此,403 Forbidden响应代码还不够。您还应该发出"用户已经存在!"在响应有效载荷中。

答案 2 :(得分:0)

操作应为GET /api/users/username:userGET /api/users?username="user"。在这种情况下,GET比POST更好。如果您只想检查是否存在,也可以使用OPTIONS或HEAD,并且您不想要响应体(但它们没有被广泛使用)。通过GET,您可以将prefer头用于相同的目的。

您可以选择返回空集合或404(如果找不到)。我认为404更好,因为在这种情况下你只等待一个结果,并且名称可能是唯一的标识符。

相关问题