π Python requests
requests is a popular Python library used to send HTTP requests to websites and APIs.
It allows Python to communicate with web servers and retrieve or send data.
π¦ Install
pip install requestsImport it:
import requestsπ₯ GET Request
Used to retrieve data from a server.
response = requests.get("https://example.com")
print(response.status_code)
print(response.text)Common status codes:
200 β Success β
404 β Not Found β
500 β Server Error β οΈπ€ POST Request
Used to send or create data on a server.
data = {"name": "Tisha"}
response = requests.post(
"https://example.com/api",
json=data
)
print(response.status_code)Common status codes:
201 β Created β
400 β Bad Request β
401 β Unauthorized ππ PUT Request
Used to update or replace existing data.
data = {"name": "Tisha", "age": 23}
response = requests.put(
"https://example.com/api/1",
json=data
)
print(response.status_code)Common status codes:
200 β Updated successfully β
404 β Not Found β
400 β Bad Request ββοΈ PATCH Request
Used to partially update existing data.
data = {"age": 24}
response = requests.patch(
"https://example.com/api/1",
json=data
)
print(response.status_code)Common status codes:
200 β Updated successfully β
404 β Not Found β
400 β Bad Request βποΈ DELETE Request
Used to delete data from a server.
response = requests.delete(
"https://example.com/api/1"
)
print(response.status_code)Common status codes:
200 β Deleted successfully β
204 β No Content β
404 β Not Found βπ Common requests Functions
requests.get() # Retrieve data
requests.post() # Send/create data
requests.put() # Update data
requests.patch() # Partially update data
requests.delete() # Delete data<div class="infographic-pyrequests">
<style>
.infographic-pyrequests {
--ink:#1a1a1a; --ink-soft:#5c5c5c; --paper:#fff; --paper-alt:#f0ece4;
--accent:#2563eb; --accent-soft:#e3ebfd; --line:#ddd6c9;
--ok:#1f9d55; --ok-soft:#e3f5ec; --err:#c0392b; --err-soft:#fbe9e7; --warn:#b45309; --warn-soft:#fdf0dc;
--mono-font:'IBM Plex Mono','SFMono-Regular',Menlo,monospace;
--display-font:'Fraunces',Georgia,serif; --body-font:'Inter',system-ui,sans-serif;
box-sizing:border-box; max-width:700px; margin:0 auto; font-family:var(--body-font); color:var(--ink); background:var(--paper);
}
.infographic-pyrequests[data-theme="dark"] {
--ink:#e8e6e1; --ink-soft:#9a968c; --paper:#121212; --paper-alt:#1e1e26;
--accent:#5b8def; --accent-soft:#202942; --line:#2c313d;
--ok:#6fdc8c; --ok-soft:#16342a; --err:#e57373; --err-soft:#3a2222; --warn:#e0a458; --warn-soft:#3a2c14;
}
.infographic-pyrequests *,.infographic-pyrequests *::before,.infographic-pyrequests *::after{box-sizing:border-box;}
.infographic-pyrequests .rq-wrap{padding:0 clamp(1rem,4vw,2rem) 1.5rem;}
.infographic-pyrequests h1{font-family:var(--display-font);font-weight:600;font-size:clamp(1.4rem,5vw,1.9rem);text-align:center;margin:1.5rem 0 0.25rem;letter-spacing:-0.01em;}
.infographic-pyrequests .rq-sub{text-align:center;color:var(--ink-soft);font-size:0.85rem;margin:0 0 1.3rem;}
.infographic-pyrequests .code-line{text-align:center;font-family:var(--mono-font);font-size:0.8rem;color:var(--ink-soft);margin-bottom:1.1rem;}
.infographic-pyrequests .code-line strong{color:var(--ink);}
.infographic-pyrequests .ep-row{display:flex;flex-wrap:wrap;justify-content:center;gap:0.4rem;margin-bottom:1rem;}
.infographic-pyrequests .ep-btn{font:inherit;font-family:var(--mono-font);font-size:0.76rem;font-weight:700;text-align:left;background:var(--paper-alt);border:1.5px solid var(--line);color:var(--ink);padding:0.42rem 0.7rem;cursor:pointer;border-radius:6px;}
.infographic-pyrequests .ep-btn.is-selected{border-color:var(--accent);background:var(--accent-soft);color:var(--accent);}
.infographic-pyrequests .run-row{display:flex;justify-content:center;margin-bottom:1rem;}
.infographic-pyrequests .run-btn{font:inherit;font-family:var(--mono-font);font-size:0.85rem;font-weight:700;background:var(--accent);color:#fff;border:none;border-radius:999px;padding:0.55rem 1.2rem;cursor:pointer;}
.infographic-pyrequests .run-btn:disabled{opacity:0.5;cursor:default;}
.infographic-pyrequests .status-row{display:flex;justify-content:center;margin-bottom:0.8rem;}
.infographic-pyrequests .status-badge{display:inline-flex;align-items:center;gap:0.5rem;font-family:var(--mono-font);font-weight:700;font-size:1.1rem;padding:0.4rem 1rem;border-radius:999px;border:2px solid var(--line);color:var(--ink-soft);}
.infographic-pyrequests .status-badge.ok-s{border-color:var(--ok);background:var(--ok-soft);color:var(--ok);}
.infographic-pyrequests .status-badge.err-s{border-color:var(--err);background:var(--err-soft);color:var(--err);}
.infographic-pyrequests .status-badge.warn-s{border-color:var(--warn);background:var(--warn-soft);color:var(--warn);}
.infographic-pyrequests .device{--ed-bg:#1e1e1e;--ed-bar:#2d2d2d;--ed-text:#d4d4d4;--ed-soft:#8a8a8a;--ed-ok:#6fdc8c;--ed-err:#ff6b6b;background:var(--ed-bg);border:1px solid var(--line);border-radius:8px;overflow:hidden;}
.infographic-pyrequests .device-bar{display:flex;align-items:center;gap:0.4rem;background:var(--ed-bar);padding:0.5rem 0.7rem;border-bottom:1px solid #3a3a3a;font-size:0.72rem;color:var(--ed-soft);font-weight:600;}
.infographic-pyrequests .device-bar .dot{width:9px;height:9px;border-radius:50%;background:#5a5a5a;}
.infographic-pyrequests .device-bar .dot:nth-child(1){background:#ff5f56;}
.infographic-pyrequests .device-body{padding:0.75rem 0.9rem;font-family:var(--mono-font);font-size:0.78rem;color:var(--ed-text);min-height:80px;white-space:pre-wrap;}
.infographic-pyrequests .rq-caption{text-align:center;font-size:0.85rem;color:var(--ink-soft);margin-top:0.9rem;}
@media (prefers-reduced-motion:reduce){.infographic-pyrequests *,.infographic-pyrequests *::before,.infographic-pyrequests *::after{animation-duration:.001ms!important;transition-duration:.001ms!important;}}
</style>
<div class="rq-wrap">
<h1>Requests</h1>
<p class="rq-sub">requests.get(url) talks to the internet β the response tells you what actually happened.</p>
<div class="code-line">response = requests.get(<strong id="urlLabel">"https://api.example.com/user/1"</strong>)</div>
<div class="ep-row">
<button class="ep-btn is-selected" data-ep="ok">/user/1 β 200 OK</button>
<button class="ep-btn" data-ep="notfound">/user/999 β 404</button>
<button class="ep-btn" data-ep="server">/broken β 500</button>
</div>
<div class="run-row"><button class="run-btn" id="runBtn">Run: requests.get(url)</button></div>
<div class="status-row"><span class="status-badge" id="statusBadge">β waiting β</span></div>
<div class="device">
<div class="device-bar"><span class="dot"></span><span class="dot"></span><span class="dot"></span> Terminal</div>
<div class="device-body" id="rqLog">Pick an endpoint, then hit Run.</div>
</div>
<p class="rq-caption" id="rqCaption">response.json() only works if the server actually sent JSON back β check status_code first.</p>
</div>
<script>
(function(){
var root = document.currentScript.closest('.infographic-pyrequests') || document.querySelector('.infographic-pyrequests');
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark = bodyAttr === 'true' ? true : bodyAttr === 'false' ? false : (document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/) || [])[1] === 'true';
if (dark) root.setAttribute('data-theme', 'dark');
var endpoints = {
ok: {
url: 'https://api.example.com/user/1',
status: 200, statusText: 'OK', badgeClass: 'ok-s',
json: { id: 1, name: 'Alice', email: 'alice@example.com' },
caption: 'response.json() only works if the server actually sent JSON back β check status_code first.'
},
notfound: {
url: 'https://api.example.com/user/999',
status: 404, statusText: 'Not Found', badgeClass: 'err-s',
json: { error: 'User not found' },
caption: 'Still a valid response! 404 just means "no such resource" β requests doesn\'t raise on its own.'
},
server: {
url: 'https://api.example.com/broken',
status: 500, statusText: 'Internal Server Error', badgeClass: 'warn-s',
json: null,
caption: 'A 500 usually means the server itself crashed β response.json() would fail here since there\'s no JSON body.'
}
};
var current = 'ok';
var urlLabel = root.querySelector('#urlLabel');
var runBtn = root.querySelector('#runBtn');
var statusBadge = root.querySelector('#statusBadge');
var rqLog = root.querySelector('#rqLog');
var rqCaption = root.querySelector('#rqCaption');
root.querySelectorAll('.ep-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
root.querySelectorAll('.ep-btn').forEach(function (b) { b.classList.remove('is-selected'); });
btn.classList.add('is-selected');
current = btn.dataset.ep;
urlLabel.textContent = '"' + endpoints[current].url + '"';
statusBadge.textContent = 'β waiting β';
statusBadge.className = 'status-badge';
rqLog.textContent = 'Pick an endpoint, then hit Run.';
});
});
runBtn.addEventListener('click', function () {
var ep = endpoints[current];
statusBadge.textContent = ep.status + ' ' + ep.statusText;
statusBadge.className = 'status-badge ' + ep.badgeClass;
var lines = [];
lines.push('>>> response = requests.get("' + ep.url + '")');
lines.push('>>> response.status_code');
lines.push(String(ep.status));
lines.push('');
if (ep.json) {
lines.push('>>> response.json()');
lines.push(JSON.stringify(ep.json, null, 2));
} else {
lines.push('>>> response.json()');
lines.push('json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)');
}
rqLog.textContent = lines.join('\n');
rqCaption.textContent = ep.caption;
});
})();
</script>
</div>