In previous lessons we always used GET requests — asking the server for data. But HTTP supports other methods for creating, updating, and deleting data as well.
The Five Core Methods
GET — Retrieve data
The most common method. Data is passed only in the URL; the request body is empty.
import requests
# Get a list of repositories
response = requests.get("https://api.github.com/user/repos", headers=headers)
When to use: reading, searching, filtering — any data retrieval that doesn’t modify anything.
POST — Create a resource
Creates a new object. Data is sent in the request body as JSON.
# Create a repository
response = requests.post(
"https://api.github.com/user/repos",
headers=headers,
json={"name": "my-new-repo", "private": False} # ← request body
)
# Success response: 201 Created
When to use: creating a new user, repository, issue, or order.
PUT — Replace a resource entirely
Replaces the entire object. You must send all fields, even the ones you’re not changing.
# Replace the entire user profile
response = requests.put(
"https://api.example.com/users/42",
json={"name": "Артём", "email": "artem@example.com", "city": "Moscow"}
)
When to use: full replacement of an object.
PATCH — Partial update
Updates only the fields you send; everything else stays the same.
# Close an issue (change only the state field)
response = requests.patch(
"https://api.github.com/repos/user/repo/issues/5",
headers=headers,
json={"state": "closed"} # ← only the field being changed
)
When to use: partial updates (changing a status, a single field).
DELETE — Remove a resource
Deletes an object. The request body is typically empty; a successful response is 204 No Content.
# Delete a repository
response = requests.delete(
"https://api.github.com/repos/user/old-repo",
headers=headers
)
# Success response: 204 No Content
When to use: deleting objects.
The Key Difference Between GET and the Rest
| GET | POST / PUT / PATCH / DELETE | |
|---|---|---|
| Data | In the URL (?key=val) |
In the request body (json={}) |
| Cached | Yes | No |
| Idempotent | Yes | POST — no; PUT/DELETE — yes |
| In a browser | Address bar | Only via code / form |
Idempotent means the result is the same no matter how many times you repeat the call. GET a list of repositories five times — you get the same list. POST “create repo” five times — you get five separate repositories (or duplicate errors).
params= vs json= in requests
# GET — data in the URL via params=
requests.get(url, params={"q": "python", "per_page": 10})
# Resulting URL: url?q=python&per_page=10
# POST — data in the body via json=
requests.post(url, json={"name": "my-repo", "private": True})
# URL has no query params; body: {"name": "my-repo", "private": true}
json= automatically:
- Serializes the dictionary to a JSON string
- Adds the Content-Type: application/json header
Status Codes by Method
| Method | Success code |
|---|---|
| GET | 200 OK |
| POST | 201 Created |
| PUT / PATCH | 200 OK |
| DELETE | 204 No Content |
CRUD — The Standard Pattern
REST APIs are built around CRUD (Create, Read, Update, Delete):
| CRUD | HTTP Method | Example |
|---|---|---|
| Create | POST | Create an issue |
| Read | GET | Get a list of issues |
| Update | PUT / PATCH | Close an issue |
| Delete | DELETE | Delete an issue |
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!