Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 324
danday74 Avatar asked Sep 15 '17 14:09

danday74


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.

like image 173
Konstantin Smolyanin Avatar answered Nov 02 '22 20:11

Konstantin Smolyanin


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.

like image 42
alexmac Avatar answered Nov 02 '22 21:11

alexmac