我应该为UPDATE(PUT)和DELETE(例如,产品成功更新)设置什么状态代码?
当前回答
RFC 2616描述了要使用的状态代码。
不,它并不总是200。
其他回答
2014年6月,RFC7231废除了RFC2616。如果您正在通过HTTP进行REST,那么RFC7231将准确描述GET、PUT、POST和DELETE的预期行为
{
"VALIDATON_ERROR": {
"code": 512,
"message": "Validation error"
},
"CONTINUE": {
"code": 100,
"message": "Continue"
},
"SWITCHING_PROTOCOLS": {
"code": 101,
"message": "Switching Protocols"
},
"PROCESSING": {
"code": 102,
"message": "Processing"
},
"OK": {
"code": 200,
"message": "OK"
},
"CREATED": {
"code": 201,
"message": "Created"
},
"ACCEPTED": {
"code": 202,
"message": "Accepted"
},
"NON_AUTHORITATIVE_INFORMATION": {
"code": 203,
"message": "Non Authoritative Information"
},
"NO_CONTENT": {
"code": 204,
"message": "No Content"
},
"RESET_CONTENT": {
"code": 205,
"message": "Reset Content"
},
"PARTIAL_CONTENT": {
"code": 206,
"message": "Partial Content"
},
"MULTI_STATUS": {
"code": 207,
"message": "Multi-Status"
},
"MULTIPLE_CHOICES": {
"code": 300,
"message": "Multiple Choices"
},
"MOVED_PERMANENTLY": {
"code": 301,
"message": "Moved Permanently"
},
"MOVED_TEMPORARILY": {
"code": 302,
"message": "Moved Temporarily"
},
"SEE_OTHER": {
"code": 303,
"message": "See Other"
},
"NOT_MODIFIED": {
"code": 304,
"message": "Not Modified"
},
"USE_PROXY": {
"code": 305,
"message": "Use Proxy"
},
"TEMPORARY_REDIRECT": {
"code": 307,
"message": "Temporary Redirect"
},
"PERMANENT_REDIRECT": {
"code": 308,
"message": "Permanent Redirect"
},
"BAD_REQUEST": {
"code": 400,
"message": "Bad Request"
},
"UNAUTHORIZED": {
"code": 401,
"message": "Unauthorized"
},
"PAYMENT_REQUIRED": {
"code": 402,
"message": "Payment Required"
},
"FORBIDDEN": {
"code": 403,
"message": "Forbidden"
},
"NOT_FOUND": {
"code": 404,
"message": "Not Found"
},
"METHOD_NOT_ALLOWED": {
"code": 405,
"message": "Method Not Allowed"
},
"NOT_ACCEPTABLE": {
"code": 406,
"message": "Not Acceptable"
},
"PROXY_AUTHENTICATION_REQUIRED": {
"code": 407,
"message": "Proxy Authentication Required"
},
"REQUEST_TIMEOUT": {
"code": 408,
"message": "Request Timeout"
},
"CONFLICT": {
"code": 409,
"message": "Conflict"
},
"GONE": {
"code": 410,
"message": "Gone"
},
"LENGTH_REQUIRED": {
"code": 411,
"message": "Length Required"
},
"PRECONDITION_FAILED": {
"code": 412,
"message": "Precondition Failed"
},
"REQUEST_TOO_LONG": {
"code": 413,
"message": "Request Entity Too Large"
},
"REQUEST_URI_TOO_LONG": {
"code": 414,
"message": "Request-URI Too Long"
},
"UNSUPPORTED_MEDIA_TYPE": {
"code": 415,
"message": "Unsupported Media Type"
},
"REQUESTED_RANGE_NOT_SATISFIABLE": {
"code": 416,
"message": "Requested Range Not Satisfiable"
},
"EXPECTATION_FAILED": {
"code": 417,
"message": "Expectation Failed"
},
"IM_A_TEAPOT": {
"code": 418,
"message": "I'm a teapot"
},
"INSUFFICIENT_SPACE_ON_RESOURCE": {
"code": 419,
"message": "Insufficient Space on Resource"
},
"METHOD_FAILURE": {
"code": 420,
"message": "Method Failure"
},
"UNPROCESSABLE_ENTITY": {
"code": 422,
"message": "Unprocessable Entity"
},
"LOCKED": {
"code": 423,
"message": "Locked"
},
"FAILED_DEPENDENCY": {
"code": 424,
"message": "Failed Dependency"
},
"PRECONDITION_REQUIRED": {
"code": 428,
"message": "Precondition Required"
},
"TOO_MANY_REQUESTS": {
"code": 429,
"message": "Too Many Requests"
},
"REQUEST_HEADER_FIELDS_TOO_LARGE": {
"code": 431,
"message": "Request Header Fields Too"
},
"UNAVAILABLE_FOR_LEGAL_REASONS": {
"code": 451,
"message": "Unavailable For Legal Reasons"
},
"INTERNAL_SERVER_ERROR": {
"code": 500,
"message": "Internal Server Error"
},
"NOT_IMPLEMENTED": {
"code": 501,
"message": "Not Implemented"
},
"BAD_GATEWAY": {
"code": 502,
"message": "Bad Gateway"
},
"SERVICE_UNAVAILABLE": {
"code": 503,
"message": "Service Unavailable"
},
"GATEWAY_TIMEOUT": {
"code": 504,
"message": "Gateway Timeout"
},
"HTTP_VERSION_NOT_SUPPORTED": {
"code": 505,
"message": "HTTP Version Not Supported"
},
"INSUFFICIENT_STORAGE": {
"code": 507,
"message": "Insufficient Storage"
},
"NETWORK_AUTHENTICATION_REQUIRED": {
"code": 511,
"message": "Network Authentication Required"
}
}
简短回答:对于PUT和DELETE,您应该发送200(OK)或204(No Content)。
长答案:这是一个完整的决策图(点击放大)。
资料来源:https://github.com/for-GET/http-decision-diagram
由于这个问题探究了DELETE“应该”返回200而不是204,因此值得考虑的是,有些人建议返回带有链接的实体,因此首选200。
“与其返回204(无内容),API应该是有用的建议去哪里。在这个例子中,我认为provide是“'somewhere.com/container/'(减去'resource')”-容器客户端刚刚删除了一个资源。也许客户希望删除更多资源,这样将是一个有用的链接。"
http://blog.ploeh.dk/2013/04/30/rest-lesson-learned-avoid-204-responses/
如果客户机遇到204响应,它可以放弃或转到API的入口点,或返回到上一个资源访问。两种选择都不是特别好。
就我个人而言,我不会说204是错误的(作者也不会;他说“令人讨厌”),因为客户端的良好缓存有很多好处。最好是保持一致。
除了200和204之外,205(重置内容)可能是有效的响应。
服务器已完成请求,用户代理应重置导致发送请求的文档视图。。。[例如]清除输入的形式。
推荐文章
- 什么是HTTP中的“406-不可接受的响应”?
- 最好的轻量级web服务器(只有静态内容)的Windows
- HTTP POST在Java中使用JSON
- 哪些HTTP方法与哪些CRUD方法相匹配?
- 使用HTML形式的PUT方法
- 如何转义哈希字符在URL
- 有没有办法在python中做HTTP PUT
- 如何用Express/Node以编程方式发送404响应?
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- 什么是http头“X-XSS-Protection”?
- 在nodejs http中body在哪里。得到回应?
- Ruby:如何将散列转换为HTTP参数?
- 在用nodejs和express创建的REST API中设置响应状态和JSON内容的正确方法
- HTTP 1.1和HTTP 2.0的区别是什么?
- 什么是“升级-不安全-请求”HTTP报头?