rest - What status code should I response with when there is no data found in my database -


i wondering status code response in else statement code below:

       if (album) {             res.status(200).json({error: false, data: {channel_id: album.attributes.channel_id, id: album.id}});         } else {             res.status(200).json({error: false, data: {message: 'there not album found name'}});         } 

i don't want leave both of them 200 want front end manage messaged thru status code, ex if returns 200 "created successfully" while in else case display "no data found".

what suggestion?

"no data found" should 404.

"created successfully" should 201.

for 201 should specify location header request can access new resource.

refs:
201 http://www.w3.org/protocols/rfc2616/rfc2616-sec10.html#sec10.2.2
404 http://www.w3.org/protocols/rfc2616/rfc2616-sec10.html#sec10.4.5

update:

i thought i'd expand on this, because comments below point thought processes i've battled myself.

get /some/thing responding 404 not found may mean database entity not found, mean there no such api end point. http doesn't differentiate these cases. url represents resource. it's not thing in considered differently thing represents.

some apis respond 400 when request made non-existant endpoint, don't seems contradict way web servers respond normal resource requests. confuse developers building client applications, suggests wrong in http transport rather in own code.

suppose decide use 404 missing database entities , 400 consider bad requests. may work you, application grows you'll uncover more , more scenarios simple status codes can't communicate on own. , point..

my suggestion all api responses carry meaningful data in body. (json or xml, or whatever you're using). request invalid url may like:

http/1.1 404 not found  { "error": "no_endpoint", "errortext":"no such api end point" } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -