-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17_http.ring
More file actions
62 lines (53 loc) · 1.54 KB
/
17_http.ring
File metadata and controls
62 lines (53 loc) · 1.54 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
17 - HTTP Requests
Use Python's urllib to make HTTP requests.
(No external dependencies required)
*/
load "python.ring"
py_init()
# ---- Simple GET request ----
? "=== GET Request ==="
py_exec("
import urllib.request
import json
url = 'https://httpbin.org/get'
req = urllib.request.Request(url, headers={'User-Agent': 'ring-python/0.1'})
with urllib.request.urlopen(req, timeout=10) as resp:
_status = resp.status
_body = json.loads(resp.read().decode())
_origin = _body.get('origin', 'unknown')
_ua = _body.get('headers', {}).get('User-Agent', 'unknown')
")
? "URL: https://httpbin.org/get"
? "Status: " + py_get("_status")
? "Origin IP: " + py_get("_origin")
? "User-Agent: " + py_get("_ua")
# ---- POST request ----
? ""
? "=== POST Request ==="
py_exec("
import urllib.request
import json
data = json.dumps({'message': 'Hello from Ring!', 'number': 42}).encode()
req = urllib.request.Request(
'https://httpbin.org/post',
data=data,
headers={'Content-Type': 'application/json', 'User-Agent': 'ring-python/0.1'},
method='POST'
)
with urllib.request.urlopen(req, timeout=10) as resp:
_post_status = resp.status
body = json.loads(resp.read().decode())
_echoed = body.get('data', '')
")
? "Status: " + py_get("_post_status")
? "Echoed: " + py_get("_echoed")
# ---- Fetch JSON API ----
? ""
? "=== JSON API ==="
py_exec("
url = 'https://httpbin.org/uuid'
with urllib.request.urlopen(url, timeout=10) as resp:
_uuid = json.loads(resp.read().decode())['uuid']
")
? "Random UUID: " + py_get("_uuid")