Avi Steiner
Avi Steiner

Reputation: 379

What http status codes should I set myself, and in what situations?

What http status codes should I set myself, and in what situations?

For instance, if the server receives a request to delete a non-existent entry from the database, should I return a 404 error code? Something else?

Upvotes: 1

Views: 95

Answers (3)

Tchaps
Tchaps

Reputation: 874

I should not return the 404 code because i thing that returning 404 most of time mean that the url doesn't exist or the page is not found when we are doing a Get request. But doing a Delete request, i would opt for a 400 Bad Request with a message error (message can explain what wrong). I think that returning a 404 status can be confusing but Bad Request will tell the of the api the URL exist but he is doing something wrong. It can be sometime frustrating if the consumer of the api does not understand very well the error that is happening. But If you do not wish to make this information available to the client, the status code 404 (Not Found) can be used instead. Me i would return a Bad Request for internal part of the application but Not Found for Public/Consumer part of of an application.

404 Not Found

This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35096

It all depends on who the client is and what functionality you require. But I might warn against 404s, as they are so synonymous with operation failure. But this might be the right way to go in your case

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359986

Assuming you're talking about designing a RESTful API, yes it makes sense to return a 404 status when you receive a request (GET, PUT, DELETE) to operate on a nonexistent entity.

Upvotes: 1

Related Questions