Not started
📨 Module 3 of 5

Reading Responses

Every API response has two parts: a status code (did it work?) and a body (here's the data). Learning to read both will make you a much more effective developer.

HTTP Status Codes

Status codes are 3-digit numbers that tell you the result of your request at a glance. You don't need to memorize them all — but knowing the major groups will save you hours of debugging.

2xx Success
200

OK

Request succeeded. Data is in the response body.

201

Created

New resource was successfully created (POST).

204

No Content

Success, but nothing to return (common for DELETE).

4xx Client Error
400

Bad Request

Something is wrong with your request — bad syntax or missing fields.

401

Unauthorized

You need to authenticate first.

403

Forbidden

You're authenticated but don't have permission.

404

Not Found

The resource doesn't exist.

409

Conflict

Request conflicts with current state — e.g. duplicate email.

422

Unprocessable Entity

Validation failed — data is structurally valid but semantically wrong.

5xx Server Error
500

Internal Server Error

Something broke on the server. Not your fault.

503

Service Unavailable

Server is down or overloaded.

Understanding JSON

Almost every modern web API returns data as JSON (JavaScript Object Notation). It looks like this:

// A JSON object — key/value pairs in curly braces

{
  "id": 1,
  "name": "Alice Johnson",
  "email": "[email protected]",
  "completed": false,
  "tags": ["api", "rest", "beginner"],
  "address": {
    "city": "Chicago",
    "state": "IL"
  }
}

JSON Data Types

TypeExampleNotes
String"Alice Johnson"Always in double quotes
Number42 or 3.14No quotes needed
Booleantrue or falseLowercase, no quotes
nullnullRepresents no value
Array["api", "rest"]Ordered list in square brackets
Object{"city": "Chicago"}Nested key/value pairs

The Anatomy of an API Response

Most well-designed APIs wrap their data in a consistent structure. Our training API uses a common pattern:

// GET /api/training/users — a list response

{
  "data": [...],       // ← The actual records you asked for
  "meta": {            // ← Information about the result set
    "total": 10,       //   How many total records exist
    "limit": 10,       //   How many were returned this time
    "offset": 0,       //   How many were skipped
    "count": 10        //   How many are in this response
  }
}

// GET /api/training/users/1 — a single resource

{
  "data": { "id": 1, "name": "Alice Johnson", ... }
}

// When something goes wrong — an error response

{
  "error": {
    "status": 404,
    "message": "User not found"
  }
}

Checking Status Codes in Code

When you make API calls in JavaScript, always check the status code — don't assume success just because the request completed:

const response = await fetch('/api/training/users/1');

// ✓ Check if the request succeeded
if (response.ok) {        // true for 200-299
  const json = await response.json();
  console.log(json.data); // Alice Johnson's record
} else {
  const error = await response.json();
  console.error(response.status, error.error.message);
}