Search⌘ K
AI Features

GET: Seeing Game Status

Explore how to define and use the GET method in a REST API to retrieve game status from a database. Understand how to convert game data into JSON format, handle timestamps, and manage server responses including 404 errors. This lesson helps you build efficient API routes that interact with the database and support client-side requests.

Using the GET method

The next method to define is the GET method, on the route for the specific game. This one is much simpler. The game_id from the route is used to query the database for the given game. If it exists, it’s converted to a dict using a custom method for the game ORM class. Otherwise, a 404 error is returned.

The custom conversion to a dict finds all attributes of the object that do not begin with an underscore, and places them into a dictionary. It also computes the game result, which will be one of the following: won, lost, or active. The dates and times must be ...