Skip to main content

Command Palette

Search for a command to run...

Activity 30: HTTP Status Codes

Published
•3 min read•View as Markdown

1xx (Informational)

Purpose: Indicate that the request has been received and the process is continuing.

  • Example: These are rare in RESTful APIs and mainly used in cases where the client needs feedback that the request is being processed (e.g., 100 Continue).

2xx (Success)

These codes indicate that the request was successfully received, understood, and accepted.

  • 200 OK:

    • When: Used when a request has been successfully processed, and the server returns the requested resource or information.

    • Why: Confirms the request was handled properly and the response body contains the data.

    • Example: Fetching user data or returning search results.

  • 201 Created:

    • When: Used when a new resource has been created as a result of a POST or PUT request.

    • Why: Informs the client that the creation was successful, and it usually includes a Location header with the URI of the newly created resource.

    • Example: Creating a new user or submitting a new post.

  • 204 No Content:

    • When: Used when the server successfully processes the request but does not need to return any content.

    • Why: Indicates that an update, deletion, or other actions have been performed without a body in the response.

    • Example: A DELETE request where the resource was removed successfully.

3xx (Redirection)

These codes indicate that the client must take additional actions to complete the request.

  • Example:

    • 301 Moved Permanently: The requested resource has been permanently moved to a new URI.

    • 302 Found: The resource has temporarily moved to a different URI.

Why: Used to guide the client to a new URL, often in cases where the original content has been moved or a resource’s location has changed.

4xx (Client Error)

These codes indicate issues with the client’s request.

  • 400 Bad Request:

    • When: Used when the server cannot process the request due to a client error (e.g., malformed request syntax).

    • Why: Indicates the client needs to modify the request before retrying.

    • Example: Submitting invalid data or missing required fields in a form.

  • 401 Unauthorized:

    • When: Used when the client must authenticate to get the requested resource.

    • Why: Indicates that authentication credentials are missing or invalid.

    • Example: Accessing a user’s profile without a valid authentication token.

  • 403 Forbidden:

    • When: Used when the client is authenticated but does not have permission to access the resource.

    • Why: Indicates that authorization is insufficient for the current user.

    • Example: Trying to access an admin-only endpoint as a regular user.

  • 404 Not Found:

    • When: Used when the requested resource does not exist on the server.

    • Why: Indicates that the server couldn’t find the resource based on the given URI.

    • Example: Trying to access a non-existent user profile.

5xx (Server Error)

These codes indicate that the server failed to fulfill a valid request.

  • 500 Internal Server Error:

    • When: Used when the server encounters an unexpected condition preventing it from fulfilling the request.

    • Why: A generic error message for server issues such as code bugs or database problems.

    • Example: When a backend service fails or there is a code exception.

  • 503 Service Unavailable:

    • When: Used when the server is temporarily unable to handle the request, often due to overload or maintenance.

    • Why: Informs the client that the server will be back up later, sometimes including a Retry-After header.

    • Example: When a server is down for updates or experiencing high traffic.