2828 continue
2929print ("Connected to" , str (esp .ssid , 'utf-8' ), "\t RSSI:" , esp .rssi )
3030
31+ # Initialize a requests object with a socket and esp32spi interface
3132requests .set_socket (socket , esp )
3233
33- # Initialize a requests object with a socket and esp32spi interface
3434TEXT_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
3638print ("Fetching text from %s" % TEXT_URL )
3739response = requests .get (TEXT_URL )
3840print ('-' * 40 )
3941
4042print ("Text Response: " , response .text )
4143print ('-' * 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 )
4448print ('-' * 40 )
4549
46- print ("Response's HTTP Status Code: %d" % response .status_code )
50+ print ("JSON Response: " , response .json () )
4751print ('-' * 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 )
5057print ('-' * 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 )
5363response .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