Skip to content

Commit 86eb530

Browse files
author
brentru
committed
add a better simpletest
1 parent 5ca40ae commit 86eb530

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

examples/requests_simpletest.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,47 @@
2828
continue
2929
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
3030

31+
# Initialize a requests object with a socket and esp32spi interface
3132
requests.set_socket(socket, esp)
3233

33-
# Initialize a requests object with a socket and esp32spi interface
3434
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
35+
JSON_GET_URL = "http://httpbin.org/get"
36+
JSON_POST_URL = "http://httpbin.org/post"
3537

3638
print("Fetching text from %s"%TEXT_URL)
3739
response = requests.get(TEXT_URL)
3840
print('-'*40)
3941

4042
print("Text Response: ", response.text)
4143
print('-'*40)
44+
response.close()
4245

43-
print("Raw Response, as bytes: {0}".format(response.content))
46+
print("Fetching JSON data from %s"%JSON_GET_URL)
47+
response = requests.get(JSON_GET_URL)
4448
print('-'*40)
4549

46-
print("Response's HTTP Status Code: %d"%response.status_code)
50+
print("JSON Response: ", response.json())
4751
print('-'*40)
52+
response.close()
4853

49-
print("Response's HTTP Headers: %s"%response.headers)
54+
data = '31F'
55+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
56+
response = requests.post(JSON_POST_URL, data=data)
5057
print('-'*40)
5158

52-
# Close, delete and collect the response data
59+
json_resp = response.json()
60+
# Parse out the 'data' key from json_resp dict.
61+
print("Data received from server:", json_resp['data'])
62+
print('-'*40)
5363
response.close()
64+
65+
json_data = {"Date" : "July 25, 2019"}
66+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
67+
response = requests.post(JSON_POST_URL, json=json_data)
68+
print('-'*40)
69+
70+
json_resp = response.json()
71+
# Parse out the 'json' key from json_resp dict.
72+
print("JSON Data received from server:", json_resp['json'])
73+
print('-'*40)
74+
response.close()

0 commit comments

Comments
 (0)