如果您在错误响应方面有一定的野心,我认为以下是一个全面的列表。
400 错误请求
对于格式错误的请求,例如,如果参数需要一个介于 0-9 和 11 之间的 int,则已发送。您可以返回它,并在响应正文中指定parameter x requires a value between 0 and 9
401未经授权
仅用于授权问题。签名可能是错误的,之前可能已经使用过随机数,发送的时间戳不在可接受的时间窗口内,再次使用响应正文来更准确地指定您对此进行响应的原因。为了澄清起见,仅将其用于与 OAuth 相关的错误。
403 禁止
明确表示完全不可能(现在或永远)根本不可能进行格式良好且经过授权的操作。例如,如果某个资源已被另一个用户锁定以供编辑:使用响应正文说出类似Another person is editing this right now, you'll have to wait mmkay?
.
403 Forbidden
也可能与试图获取资源有关。例如,假设用户可以访问资源 /resource/101212/properties.json 但不能访问 /resource/999/properties.json,那么您可以简单地Forbidden due to access rights
在响应正文中声明:
404 未找到
请求的资源不存在。或者 URL 根本没有成功映射到您服务中的 API。在响应正文中指定。
405 方法不允许
This is to represent that the API can not be called with for example GET
but another method must be used. When this is returned also you MUST return the extra response header Allow: POST, PUT, etc
.