Importance of Status Code in Web API

Importance of Status Code in Web API


1 Articles


What is HTTP Status Code?

(HTTP) Status code is issued by a server in response to a client's request made to server.The first digit of the status code defines the class of response, while the last two digits do not have any classifying or categorization role.HTTP status codes are especially important for Search Engine Optimization (SEO.) They provide direct information on the health of a website and help pinpoint possible problems with requested content.

Why do we need HTTP Status Code?

  • Whether the request worked out as expected
  • What is responsible for failed request

Common Mistakes !!

  • Don't send back 200 Ok when something is wrong
  • Don't send back 500 InternalServerError when the client makes a mistake

There are different levels of status code. Let's look at the most commonly used one's in web API.

Level Description Usage
1xx Informational ⚠️ I haven't used this so far in my API's
2xx Success ✅ This can be used in GET, POST, PUT, PATCH and DELETE endpoints
200 Success Ok() - Get Request
201 Created Created() - Post Request
204 No Content NoContent() - Put / Patch / Delete Request
3xx Redirects 🔃 I haven't used this so far in my API's. But noticed this in browser network tab when redirect happens from http to https
4xx Client Mistake ❌ This can be used to indicate the mistakes made by consumer of the API
400 Bad Request BadRequest() - Wrong data input by client
401 Not Authenticated Unauthorized() - Not signed in
403 Not Authorized Forbid() - Signed in but dont have permission
404 Not Found NotFound() - Resource not found
409 Conflict Conflict() - Error on simultaneous update
422 Unprocessable Entity UnprocessableEntity() - Error on data validation failures
5xx Internal Server Error 💥 This can be used to indicate server faults and consumer can only retry later
  • Webapi
  • Status Code