Dale Fraser
Dale Fraser

Reputation: 4758

REST based Response Status codes

I'm not sure what response code to use when the request is valid but there is no result for the given parameter

The JSON returns a success of true/false and a message

When I do a GET and there is some data I use:

200 HttpStatusCode.OK

However where there is no data should I still use OK and return the JSON success / message or should I use 400 HttpStatusCode.BadRequest to indicate something in the request is bad.

Upvotes: 0

Views: 858

Answers (3)

metacubed
metacubed

Reputation: 7271

If the operation was successful but there is really no response data, use the status 204 NO CONTENT. If an expected entity was missing, return 404 NOT FOUND. If there was some sort of internal error, return 500 SERVER ERROR.

According to the HTTP/1.1 spec,

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

Upvotes: 1

Raja Nadar
Raja Nadar

Reputation: 9489

It depends on what does it mean for the client to not have results. Typically lack of data would still be http 200 ok.

e.g. /employees etc.

However, for some scenarios you could return HTTP 404 Not Found.

Especially when the client expects a particular resource to be present.

e.g. employees/update/32

Normally any other response code (204 etc.) though technically valid and fitting, might confuse the client. Also, 400 Bad Request should also be not used, if there is nothing wrong with the request.

Upvotes: 3

user1859022
user1859022

Reputation: 2685

If ther was nothing wrong with the request but you just don't have any data (i.e. search returns 0 rows) than I would not use BadRequest:

BadRequest is sent when no other error is applicable, or if the exact error is unknown or does not have its own error code.

http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode%28v=vs.110%29.aspx

Upvotes: 0

Related Questions