-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-json.py
More file actions
32 lines (25 loc) · 921 Bytes
/
fetch-json.py
File metadata and controls
32 lines (25 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Title: Send HTTP GET Request
# Description: Make a GET request and handle the JSON response
# Author: @VasudevJaiswal
# Tags: http, api, requests, json
# Usage: data = fetch_json("https://api.example.com/data")
import urllib.request
import json
def fetch_json(url, headers=None):
"""
Send a GET request and return parsed JSON response.
Uses only standard library -- no external dependencies.
"""
req = urllib.request.Request(url)
if headers:
for key, value in headers.items():
req.add_header(key, value)
with urllib.request.urlopen(req) as response:
data = response.read().decode("utf-8")
return json.loads(data)
# Example usage
if __name__ == "__main__":
# Fetch a public API
result = fetch_json("https://api.github.com/orgs/Jaidevstudio")
print(f"Organization: {result['login']}")
print(f"Public repos: {result['public_repos']}")