我应该为UPDATE(PUT)和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请求:HTTP 200,HTTP 204应暗示“资源更新成功”。如果PUT请求创建了新资源,则返回HTTP 201。
对于DELETE请求:HTTP 200或HTTP 204应表示“已成功删除资源”。
HTTP 202也可以通过任一操作返回,并暗示该指令已被服务器接受,但尚未完全应用。稍后操作可能会失败,因此客户端不应该完全认为它是成功的。
如果客户机接收到一个它无法识别的状态代码,但它以2开头,则应将其视为200 OK。
看跌,看跌如果修改了现有资源,则应发送200(OK)或204(No Content)响应代码,以指示请求成功完成。
删除如果响应包括描述状态的实体,则成功响应应为200(OK);如果动作尚未实施,则为202(已接受);如果已实施动作但响应不包括实体,则为204(无内容)。
来源:IETF:RFC-9110方法定义
HTTP 200 OK:成功HTTP的标准响应请求。实际响应将取决于使用的请求方法。HTTP 204无内容:服务器已成功处理请求,但未返回任何内容
来源:HTTP状态代码列表:2xx成功
RFC 2616描述了要使用的状态代码。
不,它并不总是200。
{
"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"
}
}
这是一些状态代码,您应该知道这些代码。
1XX信息响应
100继续101交换协议102处理103早期提示
2XX成功
200好201已创建202已接受203非权威信息204无内容205重置内容206部分内容207多状态208已报告226使用的IM
3XX重定向
300多种选择301永久移动找到302303参见其他304未修改305使用代理306交换机代理307临时重定向308永久重定向
4XX客户端错误
400错误请求401未授权402所需付款403禁止404未找到405不允许的方法406不可接受407需要代理身份验证408请求超时409冲突410消失411所需长度412前提条件失败413有效负载太大414 URI太长415不支持的媒体类型416范围不满足417预期失败我是茶壶420方法失败421误导请求422不可处理实体423锁定424失败的依赖项426需要升级428所需的前提条件429请求太多431请求标头字段太大451因法律原因不可用
5XX服务器错误
500内部服务器错误501未实施502坏网关503服务不可用504网关超时505 Http版本不受支持506变化也协商507存储不足508检测到循环510未扩展511需要网络认证
简短回答:对于PUT和DELETE,您应该发送200(OK)或204(No Content)。
长答案:这是一个完整的决策图(点击放大)。
资料来源:https://github.com/for-GET/http-decision-diagram
推荐文章
- 什么是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报头?