I have and endpoint from a REST API that creates an entity, but the endpoint does not return any data.
Would it be ok to return a 201
status code? or it would be better to return a 204
status code?
Which one wins?
The 201
status code means that a resource has been created. The 204
status code is not suitable for this situation.
A response for a request that creates a resource should have the 201
status code and the Location
HTTP header with a URI that identifies the newly created entity. Quoting the RFC 7231, the current reference for semantics and content of the 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. [...]