-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_urllib.py
More file actions
29 lines (27 loc) · 989 Bytes
/
Copy pathtest_api_urllib.py
File metadata and controls
29 lines (27 loc) · 989 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
import urllib.request
import urllib.error
import json
try:
req1 = urllib.request.Request(
"http://localhost:8000/api/v1/auth/login",
data=json.dumps({"email":"admin@studentify.edu", "password":"admin123"}).encode('utf-8'),
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req1) as response:
resp1 = json.loads(response.read())
token = resp1["access_token"]
print("Login Success, Token obtained")
req2 = urllib.request.Request(
"http://localhost:8000/api/v1/admin/users",
headers={"Authorization": f"Bearer {token}"}
)
with urllib.request.urlopen(req2) as response2:
print("Users Request Success:", response2.read().decode('utf-8')[:100], "...")
except urllib.error.HTTPError as e:
print("HTTP Error:", e.code)
try:
print("Body:", e.read().decode('utf-8'))
except:
pass
except Exception as e:
print("Other Error:", str(e))