Understand HTTP requests, responses, status codes, headers, and the JSON data format.
HTTP (HyperText Transfer Protocol) is the language that clients and servers use to communicate. Every time your browser loads a page, submits a form, or fetches data, it sends an HTTP request to a server.
An HTTP request has four main parts:
1. Method — The action to perform (GET, POST, PUT, DELETE). This tells the server what you want to do.
2. URL — The address of the resource you want. For example, https://api.example.com/users/42 targets user 42 on the example API.
3. Headers — Metadata about the request. Headers are key-value pairs that provide additional context. Common headers include:
Content-Type: application/json — tells the server you are sending JSON dataAuthorization: Bearer abc123 — sends an authentication tokenAccept: application/json — tells the server you want JSON backUser-Agent: Mozilla/5.0... — identifies your browser4. Body — The actual data being sent (only for POST, PUT, PATCH). When you create a new user, the body contains the user's name, email, and password as JSON data.
Conceptually, a raw HTTP request looks like this:
POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer abc123
{"name": "Alice", "email": "alice@example.com"}The first line specifies the method, the path, and the HTTP version. The following lines are headers. After a blank line comes the body. Your browser constructs these requests automatically, but understanding their structure is essential for building and debugging APIs.
When a server receives a request, it processes it and sends back an HTTP response. A response also has a specific structure:
1. Status Code — A three-digit number that tells you what happened. This is the first thing you check. 200 means success, 404 means not found, 500 means the server broke. Status codes are grouped by their first digit:
2. Headers — Response metadata. The server sends back headers like:
Content-Type: application/json — the response body is JSONContent-Length: 256 — the body is 256 bytesSet-Cookie: session=abc123 — sets a cookie in the browserCache-Control: max-age=3600 — the client can cache this for 1 hour3. Body — The actual data. For an API, this is usually JSON. For a web page, it is HTML. For an image, it is binary data.
A raw HTTP response looks like this:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 45
{"id": 42, "name": "Alice", "role": "admin"}The first line has the HTTP version, status code, and a human-readable status text. Then headers, a blank line, and the body.
Memorizing the most common status codes will make you a more effective developer. Here are the ones you will encounter daily:
Success (2xx):
Redirection (3xx):
old-site.com to new-site.com.Client Errors (4xx):
Server Errors (5xx):
JSON (JavaScript Object Notation) is the standard data format for APIs. When a client and server exchange data, they almost always use JSON. It is a way to represent structured data as plain text.
JSON looks very similar to JavaScript objects, but there are strict rules:
"name", not name or 'name'"hello", not 'hello'true/false), null, arrays, or objects// or /* */ commentsHere is an example of JSON representing a user:
{
"id": 42,
"name": "Alice Johnson",
"email": "alice@example.com",
"isAdmin": false,
"age": 28,
"address": {
"city": "San Francisco",
"country": "US"
},
"hobbies": ["coding", "hiking", "reading"]
}JavaScript has two built-in methods for working with JSON:
JSON.stringify(object) — converts a JavaScript object into a JSON string. This is what happens when your server sends data to a client.JSON.parse(jsonString) — converts a JSON string back into a JavaScript object. This is what happens when your client receives data from a server.JSON won the data format wars because it is human-readable (you can look at it and understand it), lightweight (less overhead than alternatives like XML), and universal (every programming language has built-in JSON support).
<div id="output" style="font-family:monospace; padding:16px;"></div>
<script>
// A JavaScript object (what your server works with)
const user = {
id: 42,
name: "Alice Johnson",
email: "alice@example.com",
isAdmin: false,
hobbies: ["coding", "hiking"]
};
// Convert object to JSON string (server sending data)
const jsonString = JSON.stringify(user, null, 2);
// Convert JSON string back to object (client receiving data)
const parsed = JSON.parse(jsonString);
const output = document.getElementById("output");
output.innerHTML = "<strong>Original object type:</strong> " + typeof user
+ "<br><br><strong>JSON string:</strong><pre>" + jsonString + "</pre>"
+ "<strong>Parsed back — name:</strong> " + parsed.name
+ "<br><strong>Parsed back — hobbies:</strong> " + parsed.hobbies.join(", ");
</script>What status code means 'resource not found'?