Computer Science Grade 10 20 min

5. API Responses: Status Codes and Data Formats

Understand API response status codes and common data formats like JSON.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what an API response is and identify its two main components: status code and body. Differentiate between the five main classes of HTTP status codes (1xx, 2xx, 3xx, 4xx, 5xx). Identify and explain the meaning of common status codes like 200 OK, 201 Created, 400 Bad Request, 404 Not Found, and 500 Internal Server Error. Define JSON (JavaScript Object Notation) and explain its role as a common API data format. Parse a simple JSON response to extract specific pieces of data, including nested values. Construct a simple program (in pseudocode) that checks an API response's status code before attempting to process the data. Ever wonder how your weather app instantly knows the temperature, or how a game updates your high score online? 🌦️ It's al...
2

Key Concepts & Vocabulary

TermDefinitionExample API ResponseThe message sent back from a server after it receives an API request. It contains a status code indicating the outcome and a body containing the requested data (if any).When you ask a weather API for the temperature in New York, the response includes a status code (like 200) and the actual weather data (like `{"city": "New York", "temp": 75}`). HTTP Status CodeA three-digit number included in an API response that indicates the result of the request. It's a quick way to know if the request was successful, failed, or something else happened.`200 OK` means success, while `404 Not Found` means the requested resource doesn't exist. JSON (JavaScript Object Notation)A lightweight, human-readable text format for structuring...
3

Core Syntax & Patterns

Status Code Classes 1xx: Info | 2xx: Success | 3xx: Redirection | 4xx: Client Error | 5xx: Server Error Use the first digit of a status code to quickly understand the general outcome of your API request. A '2' means it worked, a '4' means you did something wrong, and a '5' means the server has a problem. JSON Syntax Rules Data is in key/value pairs. Keys are strings in double quotes. Pairs are separated by commas. `{}` hold objects, `[]` hold arrays. Follow these syntax rules strictly when reading or creating JSON data. A missing comma or quote will cause a parsing error in your program. Conditional Data Processing Pattern IF response.status_code is 2xx THEN parse(response.body) ELSE handle_error(response.status_code) Always check for a...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A program to fetch a user's city sometimes crashes. The core logic is: `response = api.get_user(123); data = parse(response.body); print(data["user"]["address"]["city"]);`. Based on the tutorial's pitfalls, what is the most likely reason for the crash and the best way to fix it?
A.The API is unreliable. The fix is to retry the request multiple times.
B.The 'address' or 'city' keys are not always present. The fix is to check the status code first, then check for the existence of each key before accessing it.
C.The JSON parser is too slow. The fix is to use a different parsing library.
D.The status code is wrong. The fix is to ignore the status code and use a try-except block.
Challenging
An API returns a user's score. A successful response is `Status: 200`, `Body: {"score": 5000}`. A 'not found' response is `Status: 404`, `Body: {"error": "User not found"}`. Which pseudocode block correctly and safely handles these possibilities?
A.data = parse(response.body); IF data["score"] THEN print(data["score"]) ELSE print(data["error"])
B.IF response.status_code == 200 THEN print(response.body["score"]) ELSE print("Error")
C.IF response.status_code == 200 THEN data = parse(response.body) print(data["score"]) ELSE error_data = parse(response.body) print(error_data["error"])
D.TRY: data = parse(response.body) print(data["score"]) CATCH Exception: print("An error occurred")
Challenging
You are debugging an API call. You send a valid request to the server and receive a `500 Internal Server Error`. Your colleague, using an identical request from their computer, gets a `200 OK` response. What is the most logical conclusion?
A.The server is experiencing an intermittent or load-related issue, and the error is not consistently reproducible.
B.Your computer's network connection is faulty.
C.Your request must have a hidden error that your colleague's does not.
D.The API has blocked your IP address.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from IV. Interacting with APIs: Data on Demand

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.