API Error Handling u0026 Response Codes Quiz Quiz

Explore essential concepts of API error handling and response codes in this interactive quiz. Assess your understanding of HTTP status codes, common error scenarios, and best practices for effective API communication.

  1. Recognizing Client Errors

    When a client sends a malformed request to an API, which HTTP status code should typically be returned?

    1. 500 Internal Server Error
    2. 400 Bad Request
    3. 200 OK
    4. 301 Moved Permanently

    Explanation: 400 Bad Request indicates that the server could not understand the request due to invalid syntax. Returning 200 OK would incorrectly imply success. 500 Internal Server Error points to a problem on the server side, not a client issue. 301 Moved Permanently is used for URL redirection, not errors in client requests.

  2. Identifying Unauthorized Requests

    A user tries to access protected resources without authentication. What response code should the API return?

    1. 404 Not Found
    2. 401 Unauthorized
    3. 302 Found
    4. 201 Created

    Explanation: 401 Unauthorized is the appropriate code when authentication is required and has not been provided or is invalid. 404 Not Found suggests the resource does not exist, which is misleading in this case. 302 Found is for temporary redirections, and 201 Created is used after a successful resource creation, both unrelated to authentication.

  3. Forbidden Actions

    If a user is authenticated but not allowed to access a specific API endpoint, which status code should be returned?

    1. 403 Forbidden
    2. 500 Internal Server Error
    3. 301 Moved Permanently
    4. 200 OK

    Explanation: 403 Forbidden indicates that the user is authenticated but lacks the necessary permissions for the requested resource. 500 Internal Server Error is unrelated, as it refers to server issues. 200 OK signifies a successful operation, which is incorrect here. 301 Moved Permanently is about URL redirection, not access control.

  4. Handling Missing Resources

    When a client requests a resource that does not exist, which HTTP status code should the API use in its response?

    1. 201 Created
    2. 503 Service Unavailable
    3. 404 Not Found
    4. 405 Method Not Allowed

    Explanation: 404 Not Found tells the client the resource was not found on the server. 201 Created is used for successful creation events, not missing resources. 405 Method Not Allowed applies when the HTTP method is not supported, and 503 Service Unavailable means the server is temporarily unable to handle requests, which is not applicable to missing resources.

  5. Structure of Error Responses

    What is a common best practice when designing API error responses?

    1. Provide file download links
    2. Only return an empty body
    3. Send HTML-formatted error pages
    4. Include both a status code and a descriptive error message

    Explanation: Including both a status code and a descriptive error message helps clients quickly understand and handle errors. Returning an empty body gives no context, making debugging difficult. File download links are irrelevant in most error scenarios. Sending HTML-formatted pages is typically not helpful in API responses, which are usually in JSON or XML.

  6. Rate Limiting Indication

    If an API user exceeds the allowed rate limit, which HTTP status code indicates this situation most appropriately?

    1. 429 Too Many Requests
    2. 200 OK
    3. 401 Unauthorized
    4. 408 Request Timeout

    Explanation: 429 Too Many Requests is explicitly designed to inform the client that they have sent too many requests in a given amount of time. 200 OK would incorrectly signal a successful request. 401 Unauthorized deals with authentication, and 408 Request Timeout is about connection delays, not excessive requests.

  7. Success vs. Error

    For a successful GET request that returns data, which HTTP status code should typically be used?

    1. 503 Service Unavailable
    2. 200 OK
    3. 401 Unauthorized
    4. 400 Bad Request

    Explanation: 200 OK signifies the request was successful and the server responded with the requested data. 503 Service Unavailable is meant for server outages. 400 Bad Request signals a client error, and 401 Unauthorized shows missing or invalid authentication—both inappropriate for a successful GET.

  8. Method Not Allowed

    If a user tries to POST to an endpoint that only supports GET, what is the correct response code?

    1. 412 Precondition Failed
    2. 405 Method Not Allowed
    3. 200 OK
    4. 201 Created

    Explanation: 405 Method Not Allowed specifically indicates that the HTTP method used is not permitted by the target resource. 200 OK and 201 Created signify successful requests, which would be incorrect here. 412 Precondition Failed relates to failed client preconditions, not HTTP methods.

  9. Unprocessable Entity

    Which status code should an API use if the server understands the request but cannot process the contained instructions, such as invalid input formats?

    1. 204 No Content
    2. 500 Internal Server Error
    3. 422 Unprocessable Entity
    4. 302 Found

    Explanation: 422 Unprocessable Entity indicates that the server understands the content type and syntax of the request but was unable to process the contained instructions. 204 No Content is for successful requests with no content in response. 302 Found is for redirection, and 500 Internal Server Error refers to generic server issues, not input processing errors.

  10. Internal Server Faults

    When an unexpected condition prevents the server from fulfilling a request, which status code is standard?

    1. 400 Bad Request
    2. 301 Moved Permanently
    3. 500 Internal Server Error
    4. 418 I'm a teapot

    Explanation: 500 Internal Server Error is used when the server encounters an unexpected issue that stops it from fulfilling the request. 301 Moved Permanently is about redirection and irrelevant to errors. 418 I'm a teapot is an unused, humorous code, and 400 Bad Request is for client mistakes, not server faults.