Tuesday, July 2, 2024

API Status Codes with Real Time Examples

HTTP 500 Series Status Codes:
  • HTTP status codes in the 500 series are server error responses.
1. 500 Internal Server Error
  • It occurs when a server meets with an unexpected condition. It indicates that the server encountered an unexpected condition that prevented it from fulfilling the request. The exact cause of the error is not specified in the response, making it a generic error message.
Example:
  • Imagine you are using an online banking application. You have logged in successfully and want to transfer money to a friend. You fill out the form with the necessary details and hit the 'Transfer' button. Instead of processing the transaction, the application shows a '500 Internal Server Error message.
Cause:
  • This could be due to several reasons, such as a bug in the application code, an issue with the server configuration, or a problem with the database connection. For instance, if the code that processes the transaction has a programming error or the database server is down, the web server might not be able to complete your request, resulting in this error.

2. 501 Not Implemented
  • It indicates that a server doesn’t recognize the requested method or is unable to process that type of request.
Example:
  • Suppose you are using a RESTful API to manage your online store inventory. You send a request using a new HTTP method called 'PATCH' to update part of the inventory data.
Cause:
  • If the server you are communicating with has not been programmed to handle the PATCH method, it will return a 501 Not Implemented error. The server might only support GET, POST, PUT, and DELETE methods, and hence does not understand or implement the PATCH method.
  
3. 502 Bad Gateway
  • It happens when a server receives an invalid response from the upstream server.
Example:
  • You are trying to access a news website. The website's server needs to fetch the latest news articles from another server (an upstream server).
Cause:
  • If the upstream server is down or returns a corrupted response, the main server cannot retrieve the necessary data and responds with a 502 Bad Gateway error. This typically happens in complex server architectures where one server relies on another to get the required information.
  
4. 503 Service Unavailable
  • It indicates that the server is temporarily down for maintenance or overloaded.
Example:
  • A popular e-commerce website announces a flash sale. As the sale goes live, thousands of users try to access the website simultaneously.
Cause:
  • The sudden surge in traffic overwhelms the server, which cannot handle the volume of requests, leading to a 503 Service Unavailable error. Alternatively, if the website administrators are performing maintenance, they might take the server offline temporarily, resulting in this error.
  
5. 504 Gateway Timeout
  • It occurs when a server doesn’t receive a response on time from the upstream server.
Example:
  • You are using an online travel booking site to book a flight. The site needs to check availability with the airline's server.
Cause:
  • If the airline's server takes too long to respond, the booking site's server might time out while waiting for the response, resulting in a 504 Gateway Timeout error. This usually occurs in distributed systems where one service depends on another for data.
 
6. 505 HTTP Version Not Supported
  • It indicates that the server doesn’t support the HTTP protocol version used in the request.
Example:
  • You are using an old web browser to access a modern web application.
Cause:
  • If your browser sends a request using HTTP/1.0, but the server only supports HTTP/1.1 or HTTP/2, it will respond with a 505 HTTP Version Not Supported error.
  

HTTP 400 Series Status Codes:
  • HTTP status codes in the 400 series are client error responses. These indicate that the request made by the client (e.g., a web browser) was incorrect or cannot be processed by the server. 
1. 400 Bad Request
  • The server cannot understand the request due to malformed syntax.
Example:
  • You try to search for a product on an e-commerce website, but you accidentally enter invalid characters in the search box. The server doesn't understand your request and returns a 400 Bad Request error.
  
2. 401 Unauthorized
  • The request requires user authentication. The client must authenticate itself to get the requested response.
Example:
  • You try to access your email account without logging in. The server responds with a 401 Unauthorized error, asking you to log in first.
  
3. 403 Forbidden
  • It is sent when a user doesn’t have permission to access the requested page.
Example:
  • You try to access a restricted page on a company's internal website without the necessary permissions. Even if you are logged in, you get a 403 Forbidden error because you don't have the right access level.
  
4. 404 Not Found
  • The server cannot find the requested resource. This is the most common error.
Example:
  • You click on a broken link or type in a URL that doesn't exist on a website. The server returns a 404 Not Found error because the page you're looking for cannot be found.
  
5. 405 Method Not Allowed
  • The request method is known by the server but is not supported by the target resource.
Example:
  • You try to submit data using a GET request instead of a POST request on a form submission page. The server responds with a 405 Method Not Allowed error.
  
6. 408 Request Timeout
  • The server did not receive a complete request message within the time that it was prepared to wait.
Example:
  • Your internet connection is slow, and it takes too long to send a request to a website. The server times out and returns a 408 Request Timeout error.
  
7. 429 Too Many Requests
  • The user has sent too many requests in a given amount of time ("rate limiting").
Example:
  • You try to log in to your account multiple times in quick succession, and the server temporarily blocks further requests, returning a 429 Too Many Requests error.

No comments:

Post a Comment