1. Introduction
When you first hear “networking” and HTTP, it may feel like requests are flying around somewhere in the fog, servers “return something,” and the browser “somehow” shows the result. In practice, everything is much more straightforward (and that’s actually nice): HTTP is an agreement about exactly how two parties exchange messages. Not “how to connect to Wi‑Fi,” not “how to lay a cable across the ocean,” but specifically: what text/bytes we send and what we consider a valid response.
The word “contract” works really well here. A contract means you don’t try to guess the server’s mood—you follow rules: “I send this, in this form, with these parameters; you respond like that, with this status and body.” And if you can read that contract, network code stops being shamanism and becomes a regular engineering task: “build request → send → parse response.”
Mini analogy
You can think of HTTP like ordering at a cafe: you say what you want (method), which exact menu item (URL/endpoint), your preferences (headers), and sometimes order details (body). The waiter brings you a receipt with a status (success or error) and, possibly, the dish (response body). If instead of “cappuccino” you say “water, but make it like coffee,” it gets weird—roughly as weird as it is for a server when you mix up the format and meaning of a request.
2. Request and response: the core HTTP pair
If you take one idea from this lecture, let it be this: HTTP is always the “request → response” pair. You send a request (request), and the server returns a response (response). Even if the response is an “error,” it is still a response. This is fundamental: an error in HTTP is often not an exception on your side, but a normal response with a status that says, “I understood you, but…”
Below is a diagram you should have in your head as naturally as “called a function → got a result”:
flowchart LR
C["Client (your program)"] -->|HTTP request| S["Server (API)"]
S -->|HTTP response| C
What it looks like “raw”
An HTTP message is a header section (lines) + sometimes a body. Don’t panic: you won’t always write this by hand, but understanding the structure is mandatory.
Example request (hypothetical):
GET /api/expenses?limit=3 HTTP/1.1
Host: example.com
Accept: application/json
Example response (hypothetical):
HTTP/1.1 200 OK
Content-Type: application/json
[{"id":1,"title":"Coffee","amount":3.5}]
Notice an important detail: the request has “what I want” (GET) and “where” (/api/expenses?...). The response has “how it ended” (200 OK) and “what we got” (the body).
3. URL and endpoint: where we go and what we ask for
A URL is often confused with a “link,” but for a client, a URL is an address of a resource. An endpoint is usually a “specific API path,” i.e., the part of the URL that describes which exact function/resource you call on the server. In developer talk, an endpoint is not philosophy; it’s shorthand: “which path are we hitting.”
To read a URL confidently, it helps to see it as a set of parts. For example:
https://api.example.com:443/v1/users/42?verbose=true#fragment
| Part | Example | What it’s for |
|---|---|---|
| scheme | |
which protocol (usually or ) |
| host | |
which server (domain) |
| port | |
which port (often hidden; for https it’s typically 443) |
| path | |
which resource/endpoint |
| query | |
request parameters “in the string” |
| fragment | |
usually not used for HTTP APIs (more of a browser thing) |
Kotlin mini-example: break a URL into parts
We are not going to the network yet; we’re just training your brain and working with strings.
fun main() {
val url = "https://api.example.com/v1/users/42?verbose=true"
val scheme = url.substringBefore("://")
val rest = url.substringAfter("://")
println("scheme=$scheme") // scheme=https
println("rest=$rest") // rest=api.example.com/v1/users/42?verbose=true
}
Yes, this is “parsing on your knee.” But it demonstrates an important idea: a URL is a string with structure, and later tools will do the same thing—just safer and more convenient.
4. HTTP method: what exactly we do
When you see GET or POST, those are not “just words,” but part of the contract: the method tells the server your intent. For the client side today, two methods are enough because they cover the vast majority of real-world scenarios.
GET means: “give me a representation of the resource.” Usually this is reading data. Ideally, GET should not change server state (i.e., be “safe”), and typically GET has no request body.
POST means: “accept data” or “create something based on this data.” Often POST is used to create a new record, start an operation, submit a form. POST usually comes with a body, and very often it is JSON.
Summary table
| Method | Typical meaning | Does it have a request body? |
|---|---|---|
|
get data | usually no |
|
send data / create | usually yes |
Kotlin mini-example: method + URL as a meaningful pair
Still no network—just capturing the idea: a request starts with “what we do” + “where.”
fun main() {
val method = "GET"
val url = "https://api.example.com/v1/expenses?limit=10"
println("$method $url") // GET https://api.example.com/v1/expenses?limit=10
}
5. Request formats: headers and body
The method and URL answer “what” and “where.” Then comes the practical part of the contract: in what form we communicate, and which exact data we send.
Headers
Headers in HTTP are additional “key: value” fields that help both sides agree on details. If the method and URL answer “what” and “where,” then headers answer “in what form,” “what constraints,” and “what transmission parameters.”
It’s important not to treat headers as “some boring boilerplate.” In real APIs, headers often decide everything: data format, language, version, authorization (though today we intentionally won’t go deep into auth).
Two headers you should memorize right away:
Accept — “which response format I want to receive.” If you expect JSON, it makes sense to say so.
Content-Type — “which format my request body is in.” This matters when you send data (for example, JSON in a POST). If you don’t specify Content-Type, the server may interpret the body incorrectly or reject it.
Kotlin mini-example: think of headers as a Map
To understand the structure, it’s very convenient to think of headers as a dictionary.
fun main() {
val headers = mapOf(
"Accept" to "application/json",
"Content-Type" to "application/json"
)
println(headers["Accept"]) // application/json
println(headers["Content-Type"]) // application/json
}
Yes, in real HTTP headers are strings, key casing is not always important, and there are nuances. But as a beginner’s mental model, Map<String, String> is a great starting point.
Body
The body of a request or response is the “payload.” If you do a GET, most often you send nothing in the body and receive a body in the response. If you do a POST, you often send a body (for example, JSON), and the response may also contain a body (for example, the created object or an error message).
Key point: a body is not necessarily a string. Technically, it’s a sequence of bytes. But in our course (and in most typical API scenarios) we will very often think of the body as text—most commonly JSON text.
Kotlin mini-example: build a JSON body as a string
We are not serializing a model yet; we’re just showing the idea “the body contains JSON.”
fun main() {
val title = "Coffee"
val amount = 3.5
val jsonBody = """{"title":"$title","amount":$amount}"""
println(jsonBody) // {"title":"Coffee","amount":3.5}
}
Later you’ll do this via kotlinx.serialization, and it will be more reliable (no need to manually track quotes), but the idea stays the same: the body is data in an agreed-upon format.
6. Status code: how the server reports the result
A status code is the first thing you should look at in a response. It answers: “how did the attempt end?” And the word attempt matters here. The server may dislike your request, may crash itself, may successfully handle everything—and all of that is expressed with numbers.
You don’t need to memorize all codes. It’s enough to confidently understand the classes:
• 2xx — success (request processed).
• 4xx — client-side error (you sent the wrong thing, to the wrong place, in the wrong format, with the wrong data).
• 5xx — server-side error (you may have done everything correctly, but the server couldn’t process the request).
Table: a few codes you’ll see all the time
| Code | Name | Typical meaning |
|---|---|---|
|
OK | everything is fine; there is a result |
|
Created | something was created (often after POST) |
|
Bad Request | the request is invalid (format/validation) |
|
Unauthorized | not authorized (usually about tokens/login) |
|
Forbidden | access denied |
|
Not Found | resource not found |
|
Internal Server Error | an error inside the server |
Kotlin mini-example: checking “is this a success?”
It’s very useful to immediately get used to the idea “success is a range.”
fun isSuccessful(status: Int): Boolean = status in 200..299
fun main() {
println(isSuccessful(200)) // true
println(isSuccessful(404)) // false
}
Psychologically, this is more important than “checking for 200.” Because there are multiple successful statuses, and your code should be ready for that (201, 204, etc.).
7. Order of operations: status → branch → interpret body
When you start writing network code, the most common mistake is trying to immediately “parse pretty JSON.” The brain wants a reward: “here is the data, here is the list, let’s go.” But networks are harsh, and very often the server returns a body that looks like text but semantically is an error. So the order of operations must be disciplined.
First you receive the response and read the status. Then you decide whether it’s success or error. Only after that do you interpret the body: either as “data” or as an “error message” (or another format, depending on the API).
This discipline is convenient to keep in mind as a small flowchart:
flowchart TD
A["Received response"] --> B["Checked status"]
B -->|2xx| C["Read body as data (e.g., JSON)"]
B -->|non-2xx| D["Read body as error/message"]
Kotlin mini-example: request result as a type
We are not making a real request, but we model a proper result contract (you already know this from sealed class).
sealed class HttpCallResult {
data class Ok(val body: String) : HttpCallResult()
data class HttpError(val status: Int, val body: String) : HttpCallResult()
}
fun handle(status: Int, body: String): HttpCallResult =
if (status in 200..299) HttpCallResult.Ok(body)
else HttpCallResult.HttpError(status, body)
fun main() {
val r = handle(404, """{"error":"not found"}""")
println(r) // HttpError(status=404, body={"error":"not found"})
}
This feels “extra” right up until the first time you start debugging: a typed result forces you not to confuse success and error.
8. Contract for the training app ExpenseTracker
So that today’s lecture is not just “about the internet in a vacuum,” let’s connect it to our hypothetical practical CLI app that tracks expenses (let’s call it ExpenseTracker). Up to this point it has lived locally: input, collections, files, JSON. Now we want to imagine that we have an external service that stores expenses in the cloud.
We are not writing network code yet. Our goal is to learn how to read and design a contract: what endpoints exist, what methods, what statuses, what bodies.
Endpoint for fetching the list of expenses
Let’s imagine the server provides an endpoint:
GET https://api.example.com/v1/expenses?limit=3
Then the “raw” interaction could be imagined like this.
Request (example):
GET /v1/expenses?limit=3 HTTP/1.1
Host: api.example.com
Accept: application/json
Response on success:
HTTP/1.1 200 OK
Content-Type: application/json
[
{"id": 1, "title": "Coffee", "amount": 3.5},
{"id": 2, "title": "Lunch", "amount": 12.0}
]
Here the contract tells us several things at once. The GET method and the lack of a request body hint: this is reading. Accept: application/json says: “we want JSON.” 200 OK says: “success.” Content-Type in the response confirms: “yes, this is JSON.”
Endpoint for adding an expense
Now, adding:
POST https://api.example.com/v1/expenses
The request body is JSON with the new expense data.
Request (example):
POST /v1/expenses HTTP/1.1
Host: api.example.com
Content-Type: application/json
Accept: application/json
{"title":"Coffee","amount":3.5}
Response on success (one option):
HTTP/1.1 201 Created
Content-Type: application/json
{"id": 10, "title":"Coffee","amount":3.5}
A very typical pattern: the server returned 201 Created and sent back the created object with an assigned id.
Kotlin mini-example: draft request data as a model
Assume we already have a model (no serialization, just structure):
data class NewExpense(
val title: String,
val amount: Double
)
fun main() {
val expense = NewExpense(title = "Coffee", amount = 3.5)
println(expense) // NewExpense(title=Coffee, amount=3.5)
}
That’s enough for today: we capture that the request body is a representation of our data, usually in JSON.
9. Common mistakes when understanding the HTTP contract
Error No. 1: assuming “if a body arrived, it means success.”
This is a very human trap: you received some text, saw curly braces, got excited, and started parsing it as data. But the server can absolutely return a JSON error with status 400 or 404. That’s why the discipline “status first, then interpret the body” saves hours of life and pounds of nerves (and nerves, as we know, are not val but more like var, and they’re easy to ruin).
Error No. 2: mixing up the meaning of GET and POST.
Beginners sometimes use POST “because it works,” or, conversely, try to send a body in GET “because it’s more convenient.” In practice, APIs almost always expect standard semantics: GET means read; POST means send/create. When you stick to that semantics, your code becomes predictable for other developers and for the server side, and it’s better to leave unpredictability to cats and quantum physics.
Error No. 3: ignoring formats and headers.
You can try to “just send text” and hope the server figures it out. Sometimes it will. Sometimes it won’t. And sometimes it will “figure it out incorrectly”—which is the most dangerous case, because you’ll get weird errors. Headers Accept and Content-Type are your way to avoid guessing and explicitly agree on a format. It’s especially important to remember Content-Type when you send a JSON body.
Error No. 4: expecting an HTTP error to be an exception by default.
In Kotlin, you’re used to: something went wrong → catch an Exception. In HTTP, the logic is different: the server can honestly return 404 as a normal response, and that’s not a “network failure,” but the result of processing the request. These are two different categories of problems: “the server responded with an error” and “we couldn’t perform the request at all.” Today we only note this at the level of meaning; in the next lectures you’ll learn to separate these cases in code.
Error No. 5: “forgetting” about the contract and trying to guess behavior from experience.
Sometimes it feels easier to try to “call the URL somehow” and then “we’ll see.” But the HTTP contract (method, URL, headers, expected statuses, body format) is what gives you stability. It makes integration reproducible: another developer can reproduce the request, you can write clear error handling, and your app behaves the same on different days of the week (even on Monday).
GO TO FULL VERSION