HTTP, JSON, REST APIs, and Python Requests
HTTP, JSON, REST APIs, and Python Requests
Overview
Modern software applications rarely operate in isolation. Web applications, mobile apps, AI systems, dashboards, and data pipelines frequently need to communicate with external services to exchange information.
This communication is typically built upon four core components:
- HTTP – The communication protocol used to transmit data.
- JSON – A lightweight format used to represent data.
- REST – A common architectural style for designing APIs.
- Requests – A Python library used to interact with APIs.
Understanding how these components work together is fundamental for backend development, data science, machine learning, automation, and AI engineering.
1. HTTP (HyperText Transfer Protocol)
Definition
HTTP is a communication protocol that enables clients and servers to exchange information over a network.
A client sends an HTTP request, and a server responds with an HTTP response.
Client ─── Request ───► Server Client ◄── Response ─── Server
Common Use Cases
- Loading web pages
- Accessing APIs
- Downloading files
- Sending form data
- Communicating with cloud services
Request Structure
An HTTP request typically contains:
Method URL Headers Body (optional)
Example:
GET /users/101 HTTP/1.1 Host: api.example.com
Response Structure
An HTTP response typically contains:
Status Code Headers Body
Example:
HTTP/1.1 200 OK Content-Type: application/json
2. JSON (JavaScript Object Notation)
Definition
JSON is a lightweight, language-independent data format used to exchange information between systems.
Although it originated from JavaScript syntax, it is now supported by virtually every modern programming language.
JSON Data Types
JSON TypePython EquivalentObjectDictionaryArrayListStringStringNumberint / floatBooleanTrue / FalseNullNone
Example JSON
{
"id": 101,
"name": "Anunaya",
"role": "AI Engineer",
"active": true
}
Working with JSON in Python
import json
user = {
"name": "Anu",
"role": "AI Engineer"
}
json_string = json.dumps(user)
python_object = json.loads(json_string)
Conversion Functions
FunctionPurposejson.dumps()Python Object → JSON Stringjson.loads()JSON String → Python Object3. APIs (Application Programming Interfaces)
Definition
An API is a set of rules that allows one software application to communicate with another.
APIs act as intermediaries between systems.
Application A
│
▼
API
│
▼
Application B
Examples
- Weather APIs
- Payment APIs
- Social Media APIs
- AI Model APIs
- Mapping APIs
Examples:
- OpenAI API
- Google Maps API
- GitHub API
- Stripe API
APIs define:
- Available operations
- Required inputs
- Expected outputs
- Authentication requirements
4. REST (Representational State Transfer)
Definition
REST is an architectural style commonly used for designing web APIs.
REST treats information as resources that can be identified using URLs.
Examples:
/users /products /orders
Individual resources are typically represented as:
/users/101 /products/55 /orders/9001
Characteristics of RESTful APIs
- Resource-oriented
- Stateless
- Uses standard HTTP methods
- Supports predictable URL structures
5. HTTP Methods in REST APIs
GET
Retrieve data.
GET /users/101
Expected outcome:
{
"id": 101,
"name": "Anunaya"
}
POST
Create a new resource.
POST /users
Request Body:
{
"name": "John"
}
PUT
Replace an existing resource.
PUT /users/101
PATCH
Partially update a resource.
PATCH /users/101
DELETE
Remove a resource.
DELETE /users/101
6. Relationship Between HTTP, JSON, and REST
These technologies solve different problems.
TechnologyResponsibilityHTTPData TransportRESTAPI Design StyleJSONData Representation
Example Flow:
Python Application
│
▼
HTTP Request
│
▼
REST API
│
▼
JSON Response
│
▼
Python Object
7. The Requests Library
Why Requests?
Python's standard library contains modules such as urllib for making HTTP requests.
However, handling requests manually can become verbose.
The requests library provides a simpler and more readable interface.
Installation:
pip install requests
8. Making GET Requests
Basic Example
import requests
response = requests.get(
"https://jsonplaceholder.typicode.com/users"
)
print(response.status_code)
Output:
200
9. Understanding Response Objects
A response object contains several useful attributes.
response.status_code response.headers response.text response.json()
Common Attributes
AttributeDescriptionstatus_codeHTTP status codeheadersResponse metadatatextRaw response contentjson()Parsed JSON response
Example:
users = response.json() print(users[0]["name"])
10. Common HTTP Status Codes
CodeMeaning200Success201Resource Created204Success (No Content)400Bad Request401Unauthorized403Forbidden404Not Found500Internal Server Error11. Sending Data Using POST Requests
Example
import requests
payload = {
"name": "Anu",
"role": "AI Engineer"
}
response = requests.post(
"https://api.example.com/users",
json=payload
)
What json= Does
The json= parameter automatically:
- Converts Python dictionaries into JSON.
- Sets the
Content-Type: application/jsonheader. - Sends the JSON data in the request body.
12. End-to-End API Workflow
The complete interaction follows a simple sequence.
Step 1: Python Application Step 2: requests Library Sends HTTP Request Step 3: REST API Receives Request Step 4: Server Processes Request Step 5: Server Returns JSON Response Step 6: requests Converts JSON Into Python Objects Step 7: Application Uses Returned Data
Key Takeaways
- HTTP is the protocol used for communication.
- JSON is the format used to exchange data.
- APIs enable communication between applications.
- REST is a popular style for designing APIs.
- Requests is Python's most widely used HTTP client library.
- Most modern applications rely on the combination of HTTP, REST APIs, JSON, and Requests.
Understanding these concepts provides the foundation for working with web development, automation, data engineering, AI systems, and modern software integrations.