Python Library || requests || JSON ||
Requests Library
| Method | Python Function | Typical REST Action | Example |
|---|---|---|---|
| GET | requests.get(url) |
Read / retrieve data | Get a list of users |
| POST | requests.post(url, json=data) |
Create a new resource | Add a new user |
| PUT | requests.put(url, json=data) |
Update entire resource | Replace user info |
| PATCH | requests.patch(url, json=data) |
Update partial resource | Change user name only |
| DELETE | requests.delete(url) |
Delete a resource | Remove a user |
| Attribute | Description | Example |
|---|---|---|
.status_code |
HTTP status returned (e.g., 200, 201, 404) |
print(response.status_code) |
.json() |
Parses the response body as JSON and returns a Python dict | data = response.json() |
.text |
Returns the raw response body as a string | print(response.text) |
.headers |
Returns response headers as a dict | print(response.headers) |
.ok |
Returns True if status code < 400 |
if response.ok: |
JSON Library
json.dump Send Python dict → JSON text
json.load Parse JSON text → Python dict
>>> Real-world analogy Requests & JSON >>>>
Think of JSON as a common language both sides agree on:
| System | Language |
|---|---|
| Python | “dict / list” objects |
| REST API | “JSON text” |
You translate before talking:
dumps()→ “speak JSON” to the APIloads()→ “translate JSON back” to Python
Without conversion, your message would be like two people speaking different languages.
| Action | Conversion Function | Example |
|---|---|---|
| Send Python dict → JSON text | json.dumps() |
requests.post(url, data=json.dumps(payload)) |
| Parse JSON text → Python dict | json.loads() |
data = json.loads(resp.text) |
| Requests shortcut | requests.post(url, json=payload) and resp.json() |
handles conversion automatically |
Example
import requests
import json
BASE_URL = "https://jsonplaceholder.typicode.com/posts"
payload = {
"title": "Network Automation",
"body": "Learning REST API with Python requests!",
"userId": 101
}
post_resp = requests.post(BASE_URL, json=payload)
print ("======== status code =============")
print(post_resp.status_code)
print ("======== response json =============")
print(post_resp.json())
print ("======== response headers=============")
print(post_resp.headers)
print ("======== ok=============")
print(post_resp.ok)
print ("======== text=============")
print(post_resp.text)
print ("======== json format=============")
print(json.dumps(post_resp.json(), indent=4))
root@ems-host:/home/a4ems/mohan# /usr/bin/python /home/a4ems/mohan/Practice/script.py
======== status code =============
201
======== response json =============
{'title': 'Network Automation', 'body': 'Learning REST API with Python requests!', 'userId': 101, 'id': 101}
======== response headers=============
{'Date': 'Mon, 06 Oct 2025 14:12:17 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '118',
'Connection': 'keep-alive', 'access-control-allow-credentials': 'true', 'access-control-expose-headers': 'Location',
'Cache-Control': 'no-cache', 'etag': 'W/"76-a2D8+9IBLlHeQ1YHBIH4Ll/aZ80"', 'expires': '-1',
'location': 'https://jsonplaceholder.typicode.com/posts/101', 'nel': '{"report_to":"heroku-nel",
"response_headers":["Via"],"max_age":3600,"success_fraction":0.01,"failure_fraction":0.1}',
'pragma': 'no-cache', 'report-to': '{"group":"heroku-nel",
"endpoints":[{"url":"https://nel.heroku.com/reports?s=BeoO2GL8GVs9ctwZbZQx163sTS%2BqGRB%2BaMDlq
4Sb60I%3D\\u0026sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d\\u0026ts=1759759937"}],"max_age":3600}',
'reporting-endpoints': 'heroku-nel="https://nel.heroku.com/reports?s=BeoO2GL8GVs9ctwZbZQx163sTS%2BqGR
B%2BaMDlq4Sb60I%3D&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&ts=1759759937"',
'Server': 'cloudflare', 'vary': 'Origin, X-HTTP-Method-Override, Accept-Encoding',
'via': '2.0 heroku-router', 'x-content-type-options': 'nosniff', 'x-powered-by': 'Express', 'x-ratelimit-limit': '1000',
'x-ratelimit-remaining': '999', 'x-ratelimit-reset': '1759759961', 'cf-cache-status': 'DYNAMIC',
'CF-RAY': '98a5c0b6facd8974-SIN', 'alt-svc': 'h3=":443"; ma=86400'}
======== ok=============
True
======== text=============
{
"title": "Network Automation",
"body": "Learning REST API with Python requests!",
"userId": 101,
"id": 101
}
======== json format=============
{
"title": "Network Automation",
"body": "Learning REST API with Python requests!",
"userId": 101,
"id": 101
}
Comments
Post a Comment