Exploring Basic Status Codes

Exploring Basic Status Codes with FastAPI

Welcome to another course of FastAPI. In this lesson, we will focus on understanding and implementing common HTTP status codes in our API endpoints. Status codes are crucial in API development as they inform clients about the result of their requests.

By the end of this unit, you'll be well-versed in using status codes to indicate the outcome of different API operations, such as fetching, creating, and deleting resources.

Recap of Setup

Before we dive deeper into status codes, let's quickly recap our FastAPI project setup, which includes a mock database and data validation using Pydantic models.

Here is the initial setup code:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

# Mock dataset of crew members
crew = [
    {"id": 1, "name": "Cosmo", "role": "Captain"},
    {"id": 2, "name": "Alice", "role": "Engineer"},
    {"id": 3, "name": "Bob", "role": "Scientist"}
]

# Pydantic model for validation
class CrewMember(BaseModel):
    name: str
    role: str

In this setup:

  • We create an instance of FastAPI named app.
  • We define a mock dataset crew, which is a list of dictionaries representing crew members.
  • We use the Pydantic library to define a CrewMember model that ensures the incoming data is structured correctly.

Understanding Status Codes

HTTP status codes are three-digit numbers sent by the server in response to a client's request. They indicate whether a specific HTTP request has been successfully completed. Essentially, these codes are part of the HTTP response and help communicate the outcome of the operation back to the client.

For example, when a client requests data from a server, the server responds with the data along with an HTTP status code. This status code serves as a message to the client, telling it whether the request was successful or if there was some issue that needs attention.

Understanding these status codes is crucial because they allow clients to handle responses appropriately and take necessary actions based on the result of the request.

Common Status Codes

Here are some basic status codes frequently used in API development:

  • 200 OK: The request was successful, and the server returned the requested data.
  • 201 Created: The request was successful, and a new resource was created.
  • 204 No Content: The request was successful, but there is no content to return.
  • 400 Bad Request: The server could not understand the request due to invalid syntax.
  • 404 Not Found: The server cannot find the requested resource.
  • 500 Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.

There are many other status codes defined in the HTTP specification, covering a wide range of scenarios. In this unit, we'll focus on some common status codes that indicate success. Don't worry about the unsuccessful status codes just yet; we'll tackle those in later lessons as we progress through the course.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal