Skip to content

Commit 46a3a47

Browse files
authored
Merge pull request #68 from jfabernathy/master
Adding new example for circuitpython requests_https
2 parents 622856f + 5bc5ddd commit 46a3a47

File tree

1 file changed

+65
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)