Lesson 2 of 12 · How the Web Talks
HTTP Requests and Responses
See what an HTTP request and response really look like: URLs, the request line, status line, headers and body, and why HTTP is stateless. With runnable Python examples.
- 8 min read
- Beginner
- Reviewed
- What HTTP and HTTPS are
- The parts of a URL
- What is inside an HTTP request and an HTTP response
- How to pull a response apart in Python
- Why HTTP is called stateless, and why that matters for AI chat
The language of the web
In the last lesson, your program sent a request and got a response. But how do two programs that have never met understand each other? They both speak the same language: HTTP.
Every web page you open and every API you call uses HTTP. Always use the https:// address of an API, especially when your requests contain passwords or keys.
The parts of a URL
A URL has several parts, and Python can split them for you:
from urllib.parse import urlparse
url = "https://api.open-meteo.com/v1/forecast?latitude=17.38&longitude=78.49"
parts = urlparse(url)
print("Scheme:", parts.scheme)
print("Host: ", parts.netloc)
print("Path: ", parts.path)
print("Query: ", parts.query)| Part | Example | Meaning |
|---|---|---|
| Scheme | https | which rules to use: HTTP or secure HTTPS |
| Host | api.open-meteo.com | which computer (server) to talk to |
| Path | /v1/forecast | which endpoint on that server |
| Query | latitude=17.38&longitude=78.49 | extra details for this request |
You will learn all about the query part in the Query Parameters lesson.
What a request looks like
An HTTP request is just text, written in a fixed layout. When your weather program ran, it sent something like this:
GET /v1/forecast?latitude=17.38&longitude=78.49¤t=temperature_2m HTTP/1.1
Host: api.open-meteo.com
Accept: application/jsonIt has up to four parts:
- The request line: the method (
GET), the path and the HTTP version. - Headers: extra information, one per line, written as
Name: value. - A blank line, which means "the headers are finished".
- A body with data to send. A
GETrequest usually has no body.
What a response looks like
The server answers in the same kind of layout:
HTTP/1.1 200 OK
Content-Type: application/json
Date: Tue, 17 Nov 2026 11:05:00 GMT
{"latitude": 17.39, "longitude": 78.46, "current": {"time": "2026-11-17T16:30", "temperature_2m": 28.5}}Then come the headers, a blank line and the body with the actual data.
Taking a response apart in Python
Because a response is text in a fixed layout, you can take it apart yourself. This is exactly what libraries such as requests do for you behind the scenes.
import json
raw = """HTTP/1.1 200 OK
Content-Type: application/json
Date: Tue, 17 Nov 2026 11:05:00 GMT
{"city": "Hyderabad", "temperature": 28.5}"""
head, body = raw.split("\n\n", 1) # the blank line separates them
lines = head.split("\n")
status_line = lines[0]
print("Status line:", status_line)
print("Status code:", status_line.split(" ")[1])
for line in lines[1:]:
name, value = line.split(": ", 1)
print("Header:", name, "=", value)
data = json.loads(body) # turn JSON text into a dictionary
print("Temperature:", data["temperature"])json.loads() turns JSON text into a Python dictionary. You will learn JSON properly in its own lesson.
Seeing a real response
Here is the same idea with a real server. The requests library has already split the response into its parts for you:
import requests
response = requests.get(
"https://api.open-meteo.com/v1/forecast",
params={"latitude": 17.38, "longitude": 78.49, "current": "temperature_2m"},
timeout=10,
)
print("Status code: ", response.status_code)
print("Content-Type:", response.headers["Content-Type"])
print("Body: ", response.text[:120])HTTP is stateless
Think of a shop assistant with no memory at all. Every time you walk up, you must say who you are and what you want, even if you were there a minute ago.
This has a big effect on AI chat apps. When you chat with a model through its API, the server does not remember the conversation. Your program sends the whole conversation so far with every new message. That is why long chats cost more: every request carries all the earlier messages again.
Common mistakes
Take an error response apart, just like an HTTP library does.
import json
raw = """HTTP/1.1 404 Not Found
Content-Type: application/json
{"error": "City not found", "city": "Atlantis"}"""
# 1. Split raw into the head and the body. They are separated by a blank line.
# 2. From the status line, print the status code as a number and the reason phrase.
# 3. Turn the body into a dictionary and print the error message.
# Output must be exactly:
# Status code: 404
# Reason: Not Found
# Error: City not found
What separates the headers from the body in an HTTP message?
Summary
- HTTP is the set of rules for writing requests and responses. HTTPS is the secure, encrypted version.
- A URL has a scheme, a host, a path and sometimes a query.
- A request has a request line, headers, a blank line and an optional body.
- A response has a status line, headers, a blank line and a body.
- HTTP is stateless: every request must carry everything the server needs.
Next: the different kinds of request, from reading data to deleting it.
Finished reading? Tick it off to track your progress.