Repository navigation
Expand file tree
/
Copy pathhttp_get.mojo
More file actions
145 lines (121 loc) Β· 5.4 KB
/
Copy pathhttp_get.mojo
File metadata and controls
145 lines (121 loc) Β· 5.4 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Example 05 β HTTP client: GET, POST, PUT, PATCH, DELETE with flare.http.
Demonstrates:
- Plain HTTP GET (http://)
- HTTPS GET with JSON response (https://)
- Response status, headers, and body decoding
- POST, PUT, PATCH with JSON and binary bodies
- DELETE and HEAD
- Graceful skip when the network is unavailable
Run:
pixi run example-http
"""
from flare.http import HttpClient, Status, get, post, put, patch, delete, head
def main() raises:
print("=== flare Example 05: HTTP Methods ===")
print()
var client = HttpClient()
# ββ 1. Plain HTTP GET βββββββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 1. HTTP GET (plain) ββ")
try:
var resp = client.get("http://httpbin.org/get")
print(" status :", resp.status, "ok:", resp.ok())
print(" Content-Type :", resp.headers.get("content-type"))
print(
" body snippet :",
String(unsafe_from_utf8=resp.text().as_bytes()[:80]),
"...",
)
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 2. HTTPS GET β JSON βββββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 2. HTTPS GET β JSON ββ")
try:
var resp = client.get("https://httpbin.org/json")
print(" status :", resp.status, "ok:", resp.ok())
print(" Content-Type :", resp.headers.get("content-type"))
print(
" body snippet :",
String(unsafe_from_utf8=resp.text().as_bytes()[:80]),
"...",
)
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 3. 404 response is not ok() βββββββββββββββββββββββββββββββββββββββββββ
print("ββ 3. 404 not found ββ")
try:
var resp = client.get("http://httpbin.org/status/404")
print(" status :", resp.status, "ok:", resp.ok())
print(" ok() is False β expected:", not resp.ok())
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 4. POST with JSON body ββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 4. POST with JSON body ββ")
try:
var resp = client.post(
"https://httpbin.org/post",
'{"hello": "flare"}',
)
print(" status :", resp.status, "ok:", resp.ok())
print(
" body snippet :",
String(unsafe_from_utf8=resp.text().as_bytes()[:120]),
"...",
)
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 5. PUT with JSON body βββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 5. PUT with JSON body ββ")
try:
var resp = client.put(
"https://httpbin.org/put",
'{"action": "update"}',
)
print(" status :", resp.status, "ok:", resp.ok())
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 6. PATCH with JSON body βββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 6. PATCH with JSON body ββ")
try:
var resp = client.patch(
"https://httpbin.org/patch",
'{"field": "patched"}',
)
print(" status :", resp.status, "ok:", resp.ok())
print(
" body snippet :",
String(unsafe_from_utf8=resp.text().as_bytes()[:120]),
"...",
)
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 7. DELETE βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 7. DELETE ββ")
try:
var resp = client.delete("https://httpbin.org/delete")
print(" status :", resp.status, "ok:", resp.ok())
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 8. HEAD (no body returned) ββββββββββββββββββββββββββββββββββββββββββββ
print("ββ 8. HEAD (no body) ββ")
try:
var resp = client.head("http://httpbin.org/get")
print(" status :", resp.status, "body bytes:", len(resp.body))
except e:
print(" [SKIP] network unavailable:", String(e))
print()
# ββ 9. Module-level one-shot patch() βββββββββββββββββββββββββββββββββββββ
print("ββ 9. Module-level patch() ββ")
try:
var resp = patch("https://httpbin.org/patch", '{"one-shot": true}')
print(" status :", resp.status, "ok:", resp.ok())
except e:
print(" [SKIP] network unavailable:", String(e))
print()
print("=== Example 05 complete ===")