⭐ Please help by giving this repository a star. It takes seconds and help thousands of developers! ⭐

I Love .NET Star Request

🤝🏿 Please share with the community! 🤝🏿

As an open-source project without funding, I cannot afford advertising I ❤️ .NET in a typical way. Instead, the project relies on community interactions. Please consider sharing a post about I ❤️ .NET and the value it provides. It really does help!

Importance of Status Code in Web API

Importance of Status Code in Web API

webapi

3 Articles

Improve

In this article, let's learn about importance of status code in Web API.

Table of Contents

  1. What is HTTP Status Code?
  2. Why do we need HTTP Status Code?
  3. Common Mistakes !!
  4. Summary

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

Summary

I'm happy that you have reached to the end of this article. Here we learnt what is HTTP Status Code and why do we need it and what are the common mistakes we do when it comes to returning status codes. We also saw a list of status codes. Hope you find this useful.

  • Webapi
  • Status Code