Sobes.tech
Junior

Tell about HTTP status codes.

sobes.tech AI

Answer from AI

HTTP status codes are standard three-digit numbers returned by the server in response to a client request. They indicate the result of the request processing.

Categories of status codes:

  • 1xx (Informational): The server has received the request and is continuing the process.
  • 2xx (Success): The request was successfully processed.
  • 3xx (Redirection): Additional action is needed to complete the request.
  • 4xx (Client Error): The request contains a syntax error or cannot be processed.
  • 5xx (Server Error): The server failed to fulfill a valid request.

Common status codes:

Code Name Description
200 OK The request was successfully processed.
201 Created The resource was successfully created.
204 No Content The request was successfully processed, but no content to return.
301 Moved Permanently The resource has been permanently moved to a new URL.
302 Found The resource is temporarily available at a different URL.
304 Not Modified The resource has not been modified since the last request.
400 Bad Request The server could not understand the request due to invalid syntax.
401 Unauthorized Authentication is required.
403 Forbidden Access to the resource is forbidden.
404 Not Found The requested resource was not found.
405 Method Not Allowed The HTTP method used is not allowed for the resource.
409 Conflict Conflict occurred while processing the request, e.g., creating an existing resource.
422 Unprocessable Entity The request is syntactically correct but semantically invalid.
500 Internal Server Error Internal server error.
501 Not Implemented The server does not support the functionality required to fulfill the request.
503 Service Unavailable The server is temporarily unavailable.

During API testing, status codes are a key indicator of success or failure. Checking the correct status code for each request type (GET, POST, PUT, DELETE, etc.) in various scenarios (success, validation error, resource not found, authorization) is fundamental to API functional testing.

For example, creating a new resource (POST request) expects a 201 Created status, while requesting a non-existent resource (GET request) expects a 404 Not Found.

import requests

# Example of checking status code for a successful request
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
# Expect status 200 OK
assert response.status_code == 200

# Example of checking status code for a non-existent resource
response_not_found = requests.get('https://jsonplaceholder.typicode.com/posts/9999')
# Expect status 404 Not Found
assert response_not_found.status_code == 404