post with no response body, which response code is better 201 or 204?

Viewed 9069

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?

2 Answers

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.

Related