import network
import time
import urequests
import ujson
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Example GET request
try:
response = urequests.get('http://api.example.com/data')
data = response.json() # Parse JSON response
print("Received data:", data)
response.close() # Close the response object to free up resources
except Exception as e:
print("Error making GET request:", e)
payload = {'key': 'value'}
try:
response = urequests.post('http://api.example.com/submit', json=payload)
print("POST response status:", response.status_code)
response.close()
except Exception as e:
print("Error making POST request:", e)