Skip to content

Commit 90782c8

Browse files
committed
Adding new example for circuitpython requests_https
1 parent 7cce618 commit 90782c8

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# adafruit_requests usage with a CircuitPython socket
2+
# this has been tested with Adafruit Metro ESP32-S2 Express
3+
4+
import socketpool
5+
import ssl
6+
import adafruit_requests as requests
7+
import wifi
8+
import secrets
9+
10+
# Get wifi details and more from a secrets.py file
11+
try:
12+
from secrets import secrets
13+
except ImportError:
14+
print("WiFi secrets are kept in secrets.py, please add them there!")
15+
raise
16+
17+
print("Connecting to %s"%secrets["ssid"])
18+
wifi.radio.connect(secrets["ssid"], secrets["password"])
19+
print("Connected to %s!"%secrets["ssid"])
20+
print("My IP address is", wifi.radio.ipv4_address)
21+
22+
socket = socketpool.SocketPool(wifi.radio)
23+
https = requests.Session(socket, ssl.create_default_context())
24+
25+
TEXT_URL = "https://httpbin.org/get"
26+
JSON_GET_URL = "https://httpbin.org/get"
27+
JSON_POST_URL = "https://httpbin.org/post"
28+
29+
print("Fetching text from %s" % TEXT_URL)
30+
response = https.get(TEXT_URL)
31+
print("-" * 40)
32+
print("Text Response: ", response.text)
33+
print("-" * 40)
34+
response.close()
35+
36+
print("Fetching JSON data from %s" % JSON_GET_URL)
37+
response = https.get(JSON_GET_URL)
38+
print("-" * 40)
39+
40+
print("JSON Response: ", response.json())
41+
print("-" * 40)
42+
43+
data = "31F"
44+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
45+
response = https.post(JSON_POST_URL, data=data)
46+
print("-" * 40)
47+
48+
json_resp = response.json()
49+
# Parse out the 'data' key from json_resp dict.
50+
print("Data received from server:", json_resp["data"])
51+
print("-" * 40)
52+
53+
json_data = {"Date": "July 25, 2019"}
54+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
55+
response = https.post(JSON_POST_URL, json=json_data)
56+
print("-" * 40)
57+
58+
json_resp = response.json()
59+
# Parse out the 'json' key from json_resp dict.
60+
print("JSON Data received from server:", json_resp["json"])
61+
print("-" * 40)

0 commit comments

Comments
 (0)