28
28
continue
29
29
print ("Connected to" , str (esp .ssid , 'utf-8' ), "\t RSSI:" , esp .rssi )
30
30
31
+ # Initialize a requests object with a socket and esp32spi interface
31
32
requests .set_socket (socket , esp )
32
33
33
- # Initialize a requests object with a socket and esp32spi interface
34
34
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"
35
37
36
38
print ("Fetching text from %s" % TEXT_URL )
37
39
response = requests .get (TEXT_URL )
38
40
print ('-' * 40 )
39
41
40
42
print ("Text Response: " , response .text )
41
43
print ('-' * 40 )
44
+ response .close ()
42
45
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 )
44
48
print ('-' * 40 )
45
49
46
- print ("Response's HTTP Status Code: %d" % response .status_code )
50
+ print ("JSON Response: " , response .json () )
47
51
print ('-' * 40 )
52
+ response .close ()
48
53
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 )
50
57
print ('-' * 40 )
51
58
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 )
53
63
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