HTTP Status Codes indicate the result of an API request.
They're represented as three-digit codes.
There are different types of HTTP status codes:
- 1xx informational responses
- 2xx success responses
- 3xx redirection responses
- 4xx client error responses
- 5xx server error responses
Which HTTP status code you should use depends on what you want to communicate to your API clients. They're used to communicate if the request was successful or not. HTTP status codes also carry semantic information.
Some of the most popular HTTP Status Codes are:
- 200 OK
- 400 Bad Request
- 404 Not Found
- 500 Internal Server Error
But just returning random status codes won't help.
So here are 5 best practices I use for HTTP status codes:
- 201 when creating a new resource
- 400 when there is a validation error
- 404 when a resource is not found
- 409 when a resource already exists
- Include a Problem Details response for errors
Learn more about HTTP Status Codes here: [https://blog.postman.com/what-are-http-status-codes/]