我有一个来自创建实体的 REST API 的端点,但该端点不返回任何数据。
返回 201
状态码可以吗?或者返回一个 204
状态码会更好?
谁赢了?
最佳答案
201
状态码表示资源已创建。 204
状态码不适合这种情况。
对创建资源的请求的响应应具有 201
状态代码和 Location
HTTP header 以及标识新创建实体的 URI。引用 RFC 7231 , HTTP/1.1 协议(protocol)的语义和内容的当前引用:
The
201
(Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either aLocation
header field in the response or, if noLocation
field is received, by the effective request URI. [...]
关于http - 创建但不返回数据的端点的状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40050779/