PYTHON FOR AI • LESSON 5

HTTP

HTTP is the communication system that allows applications to communicate with servers over the internet. Before working with APIs in Python, you need to understand how HTTP requests and responses work.

CORE IDEA

HTTP is how a client communicates with a server.

When your Python application needs information from another application, it sends an HTTP request. The server processes that request and sends an HTTP response back.

01

What Is HTTP?

HTTP stands for HyperText Transfer Protocol.

It is a set of rules that defines how clients and servers communicate.

For example, when you open a website:

Your Browser
      ↓
HTTP Request
      ↓
Web Server
      ↓
HTTP Response
      ↓
Your Browser

The browser is the client, and the server is the computer that processes the request.

02

Client and Server

The two most important concepts are client and server.

Client

The client is the application that makes the request.

Examples:

  • Web browser
  • Python application
  • Mobile application
  • Frontend application

Server

The server receives the request, processes it, and sends a response.

Client
  │
  │ Request
  ↓
Server
  │
  │ Response
  ↓
Client
03

What Is an HTTP Request?

An HTTP request is a message sent from a client to a server asking the server to perform an action or return information.

For example:

GET /users

This can be understood as:

GET
 ↓
I want to retrieve data

/users
 ↓
I want the users resource
04

What Is an HTTP Response?

After receiving a request, the server sends an HTTP response.

A response normally contains a status code and may contain data.

HTTP/1.1 200 OK

{
    "name": "John",
    "age": 25
}

Here:

  • 200 means the request succeeded.
  • The JSON contains the requested data.
05

HTTP Methods

HTTP methods tell the server what type of operation the client wants to perform.

GET

Used to retrieve data.

GET /products

Meaning: "Give me the products."

POST

Used to send data to create something.

POST /users

Meaning: "Create a new user."

PUT

Used to replace or update an existing resource.

PUT /users/10

Meaning: "Update user 10."

PATCH

Used to partially update an existing resource.

PATCH /users/10

For example, update only the user's email.

DELETE

Used to delete a resource.

DELETE /users/10

Meaning: "Delete user 10."

06

URL

A URL tells the client where the server resource is located.

https://example.com/products

The main parts are:

https://example.com/products
│       │           │
│       │           └── Resource
│       │
│       └── Domain
│
└── Protocol

When working with APIs, the URL usually points to an API endpoint.

https://api.example.com/products
07

What Is an API Endpoint?

An endpoint is a specific URL where an API provides a particular resource or operation.

For example:

GET /products
GET /products/10
GET /users
GET /orders

These can represent different API endpoints.

Think of an endpoint as a specific door into an API.

08

HTTP Status Codes

The server uses status codes to tell the client what happened to the request.

2xx — Success

200 OK
201 Created
204 No Content

The request was successfully processed.

4xx — Client Error

400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
429 Too Many Requests

Something is wrong with the request or the client does not have permission to perform the operation.

5xx — Server Error

500 Internal Server Error
502 Bad Gateway
503 Service Unavailable

The server or an upstream service encountered a problem.

09

HTTP Request Using Python

Python can communicate with HTTP servers using libraries such as requests.

Install it with:

pip install requests

Then:

import requests

response = requests.get(
    "https://httpbin.org/get"
)

print(response.status_code)
print(response.text)

The important part is:

requests.get(...)

This sends an HTTP GET request to the server.

10

Understanding the Response

The response object contains information returned by the server.

import requests

response = requests.get(
    "https://httpbin.org/get"
)

print(response.status_code)
print(response.headers)
print(response.text)

Three useful properties are:

  • status_code — HTTP status code
  • headers — information about the response
  • text — response body as text
11

HTTP and JSON

APIs commonly return data as JSON.

Python's Requests library can convert a JSON response into Python data using .json().

import requests

response = requests.get(
    "https://httpbin.org/json"
)

data = response.json()

print(data)

This is an important pattern that you will use throughout the rest of this course:

Python
  ↓
HTTP Request
  ↓
API
  ↓
HTTP Response
  ↓
JSON
  ↓
Python Dictionary / List
12

Real-World Example

Imagine a weather application.

The Python application wants today's weather.

Python Weather App
        │
        │ GET /weather
        ↓
Weather API
        │
        │ 200 OK
        │
        │ JSON
        ↓
Python Weather App
        │
        ↓
Display Weather

The same concept is used by AI applications.

Python AI Application
        │
        │ HTTP Request
        ↓
AI API
        │
        │ HTTP Response
        ↓
JSON Response
        │
        ↓
Python Application
13

HTTP vs API

These two concepts are related, but they are not the same thing.

HTTP

HTTP is the communication protocol.

GET
POST
PUT
PATCH
DELETE

API

An API defines what functionality another application can access.

GET /users
POST /users
GET /products
DELETE /orders/10

In simple terms:

HTTP
 ↓
Rules for communication

API
 ↓
Functionality exposed by an application
14

Complete Python Example

Here is a small example that brings the basic concepts together.

import requests


url = "https://httpbin.org/get"


response = requests.get(
    url,
    timeout=10
)


print("Status Code:")
print(response.status_code)


print("\nResponse Headers:")
print(response.headers)


print("\nResponse Data:")
print(response.json())

The process is:

1. Create URL
      ↓
2. Send GET request
      ↓
3. Server processes request
      ↓
4. Server sends response
      ↓
5. Check status code
      ↓
6. Read response data
15

What You Learned

  • What HTTP is
  • Client and server communication
  • HTTP requests
  • HTTP responses
  • URLs and API endpoints
  • GET, POST, PUT, PATCH and DELETE
  • HTTP status codes
  • Using HTTP from Python
  • Reading JSON responses
  • How HTTP is used by AI applications
Client
  ↓
HTTP Request
  ↓
API / Server
  ↓
HTTP Response
  ↓
JSON Data
  ↓
Python Application
KEY TAKEAWAY

HTTP is the communication foundation behind APIs.

When Python needs to communicate with another application, it can send an HTTP request to an API. The API processes the request and returns an HTTP response, commonly containing JSON data. Once you understand this flow, Requests, REST APIs, authentication, and eventually LLM APIs become much easier to understand.