To make HTTP requests, we are using node's restful.js
It is giving a warning:
You should return a 204 status code with an empty body.
Our request is a POST (which obviously creates a resource).
The request is successful. The response body is empty. Our response code is 201.
Should we be returning a 201 (created) or 204 (no content) response code?
Is restful.js just being overzealous?
Actually selected answer doesn't take into consideration that response body will be empty.
Response with code 201 Created not only means "created" but "the new resource is returned in the body of the message".
Description of code 201 in RFC 7231: https://httpwg.org/specs/rfc7231.html#status.201
So, it seems, that for successful response with empty body 204 No Content suits better.
Update: From the other hand it is untypical for POST operations return empty body because usually in REST APIs POST requests not only create resource but return created resource in reply. So body is not empty and the HTTP code is 201.
It depends on the type of operation that you are performing. If you're creating new object, return 201 status, updating or deleting - 204.
201 Created
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 a Location header field in the response or, if no Location field is received, by the effective request URI.
204 No Content
The server has successfully fulfilled the request and that there is no additional content to send in the response payload body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With