DEV Community

BK Mahapatra
BK Mahapatra

Posted on

Unveiling HTTP Methods and Status Codes

Image by storyset on Freepik

Welcome, fellow developers, to a journey through the heart and soul of the web – HTTP status codes and methods. This blog is your passport to understanding these crucial elements that govern the way our applications communicate. Whether you're new to the world of web development or already navigating its waters, this guide is designed to be your friendly companion.

Why It Matters

Understanding HTTP status codes and methods isn't just about memorizing numbers; it's about wielding the power to build robust, efficient, and user-friendly applications. These codes and methods are the language through which servers and clients communicate, providing insights into the success or challenges of a request.

HTTP Methods:

GET: The Explorer

  • Purpose: Retrieve data from the server.
  • Example: "Fetch details about this article."

    What we can pass:

    • Query parameters in the URL: api/articles?category=tech
    • Headers: Including information like authorization tokens.

    What we cannot pass:

    • Request body: GET requests typically do not include a request body as they are designed for retrieving information rather than sending data.
    • Sensitive data: Avoid passing sensitive information, such as passwords or API keys, directly in the URL.

POST: The Creator

  • Purpose: Send data to the server to create a new resource.
  • Example: "Submit a new comment to the blog post."

    What we can pass:

    • Request body: JSON or form data containing the information to create the new resource.
    • Headers: Including information like content type or authorization tokens.

    What we cannot pass:

    • Query parameters in the URL: POST requests usually don't have query parameters in the URL, as they are more commonly used for data creation.
    • Idempotent actions: Avoid using POST for actions that should be idempotent (i.e., producing the same result regardless of how many times the operation is repeated).

PUT: The Replacer

  • Purpose: Replace or create a resource at a specific URL.
  • Example: "Update the user profile information."

    What we can pass:

    • Request body: JSON or form data containing the updated information.
    • Headers: Including information like content type or authorization tokens.

    What we cannot pass:

    • Query parameters in the URL: While it's technically possible, it's uncommon to include them in a PUT request.
    • Partial updates: PUT is designed for full resource replacement, so partial updates might not be well-supported.

DELETE: The Eraser

  • Purpose: Delete the resource at a given URL.
  • Example: "Remove a product from the shopping cart."

    What we can pass:

    • Headers: Including information like authorization tokens.

    What we cannot pass:

    • Request body: DELETE requests typically don't include a request body as they are designed for resource deletion.
    • Undelete or undo actions: Once a resource is deleted with DELETE, retrieving it or undoing the action may require a different approach.

PATCH: The Modifier

  • Purpose: Apply partial modifications to a resource.
  • Example: "Edit specific details of a user profile."

    What we can pass:

    • Request body: JSON containing the specific modifications to be applied.
    • Headers: Including information like content type or authorization tokens.

    What we cannot pass:

    • Query parameters in the URL: Similar to PUT requests, it's uncommon to include them in a PATCH request.
    • Ambiguous updates: PATCH is designed for partial updates, but care should be taken to ensure the intended changes are clear and unambiguous.

HTTP Status Codes:

1xx Informational:

  1. 100 Continue:
    • Purpose: Indicates that the initial part of the request has been received, and the client should proceed with the rest.
    • Example: Used in scenarios where the server wants to check authentication credentials before accepting the request body.
  2. 101 Switching Protocols:
    • Purpose: The server acknowledges the client's request to switch protocols and is doing so.
    • Example: Commonly used during WebSocket protocol upgrades.
  3. 102 Processing:
    • Purpose: The server is still processing the request, and the client should continue waiting.
    • Example: Useful for long-running operations to keep the client informed about the ongoing process.

2xx Success:

  1. 200 OK:
    • Purpose: The standard response for a successful HTTP request.
    • Example: Used when fetching data or completing a successful POST request.
  2. 201 Created:
    • Purpose: Indicates that the request has been fulfilled, resulting in the creation of a new resource.
    • Example: Commonly returned after successfully submitting a form or creating a new user account.
  3. 202 Accepted:
    • Purpose: The request has been accepted but has not been fully processed yet.
    • Example: Suitable for asynchronous processing scenarios.
  4. 204 No Content:
    • Purpose: The server successfully processed the request but does not need to return an entity-body.
    • Example: Often used in DELETE requests.
  5. 206 Partial Content:
    • Purpose: Sent as part of a partial GET request, indicating that the server is delivering only a portion of the requested content.
    • Example: Used in scenarios where a large file is being downloaded in parts.

3xx Redirection:

  1. 300 Multiple Choices:
    • Purpose: The requested resource has multiple representations, and the user or user agent can select from them.
    • Example: Useful when content negotiation is possible.
  2. 301 Moved Permanently:
    • Purpose: The requested resource has been permanently moved to a new location.
    • Example: Commonly used for SEO-friendly URL redirection.
  3. 302 Found:
    • Purpose: Indicates a temporary redirection to another URI.
    • Example: Often used in temporary scenarios where a resource is temporarily moved.
  4. 304 Not Modified:
    • Purpose: Indicates that the resource has not been modified since the version specified in the request headers.
    • Example: Used to optimize caching.
  5. 307 Temporary Redirect:
    • Purpose: Similar to 302 Found but explicitly specifies that the request should be repeated using the same method.
    • Example: Useful when the resource's location is temporarily changed.
  6. 308 Permanent Redirect:
    • Purpose: Similar to 301 Moved Permanently but explicitly specifies that the request should be repeated using the same method.
    • Example: Provides a permanent redirection to another URI.

4xx Client Errors:

  1. 400 Bad Request:
    • Purpose: The server cannot or will not process the request due to a client error.
    • Example: Commonly seen when the client sends invalid data in the request.
  2. 401 Unauthorized:
    • Purpose: Similar to 403 Forbidden but specifically indicates that authentication is required and has failed or has not been provided.
    • Example: Shown when accessing a protected resource without proper authentication.
  3. 403 Forbidden:
    • Purpose: The client does not have the necessary permission to access the requested resource.
    • Example: Displayed when attempting to access a resource without the required credentials.
  4. 404 Not Found:
    • Purpose: The requested resource could not be found on the server.
    • Example: Shown when navigating to a URL that does not exist.
  5. 405 Method Not Allowed:
    • Purpose: The method specified in the request is not allowed for the requested resource.
    • Example: Occurs when trying to use an unsupported HTTP method.
  6. 409 Conflict:
    • Purpose: Indicates that the request could not be completed due to a conflict with the current state of the target resource.
    • Example: Commonly seen in scenarios where resource updates result in conflicts.
  7. 410 Gone:
    • Purpose: Similar to 404 Not Found but indicates that the requested resource is no longer available and will not be available again.
    • Example: Used when a resource has been deliberately removed.
  8. 429 Too Many Requests:
    • Purpose: The user has sent too many requests in a given amount of time.
    • Example: Implemented to prevent abuse, such as excessive API requests.

5xx Server Errors:

  1. 500 Internal Server Error:
    • Purpose: A generic error message indicating an unexpected condition that prevented the server from fulfilling the request.
    • Example: Typically used when there's an unhandled exception on the server side.
  2. 501 Not Implemented:
    • Purpose: The server does not support the functionality required to fulfill the request.
    • Example: Displayed when accessing an endpoint or feature that has not been implemented.
  3. 502 Bad Gateway:
    • Purpose: The server, while acting as a gateway or proxy, received an invalid response from an upstream server.
    • Example: Commonly seen in scenarios involving server-to-server communication.
  4. 503 Service Unavailable:
    • Purpose: The server is not ready to handle the request. Common causes include temporary overloading or maintenance of the server.
    • Example: Displayed during server downtime or maintenance.
  5. 504 Gateway Timeout:
    • Purpose: The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.
    • Example: Occurs when a server acting as a gateway or proxy times out while waiting for a response from another server.
  6. 505 HTTP Version Not Supported:
    • Purpose: The server does not support the HTTP protocol version used in the request.
    • Example: Shown when attempting to use an outdated or unsupported version of the HTTP protocol.

Conclusion

To wrap it up, when working with HTTP methods and status codes, be careful with sensitive info, watch out for irreversible actions, and communicate clearly for partial updates. Stick to consistent API design, handle errors robustly, and document everything for smoother integration. Prioritize security by using HTTPS and keeping protocols up-to-date. By following these practices, your journey in web development stays secure and efficient. Happy coding!

Top comments (0)