I'm unable to make MicroPython REST API requests from the Pico W. The WLAN connection is working but the API call fails. ► API call| Not working - Wokwi Pico W Simulator
# Import modules
import network
import urequests
# Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
pass
print('Connected to WLAN')
# Make an API call
response = urequests.get('https://official-joke-api.appspot.com/random_joke')
data = response.json()
print(data['setup'])
print(data['punchline'])
Error 1 Screenshot and text:
Connected to WLAN
Traceback (most recent call last):
File "main.py", line 14, in <module>
File "urequests.py", line 180, in get
File "urequests.py", line 93, in request
OSError: (-29312, 'MBEDTLS_ERR_SSL_CONN_EOF')
MicroPython v1.19.1-993-g283c1ba07 on 2023-03-29; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
Intermittent Error 2 Screenshot and text:
Connected to WLAN
Traceback (most recent call last):
File "main.py", line 14, in <module>
File "urequests.py", line 180, in get
File "urequests.py", line 130, in request
ValueError: HTTP error: BadStatusLine:
[]
MicroPython v1.19.1-993-g283c1ba07 on 2023-03-29; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
However, the same thing works on ESP32► Joke API - Wokwi ESP32 Simulator
And the code is working on Python too ► Python Playground - Python API call|Sololearn
urequests.get()
seem to have executed without error. This is a bit like complaining that your phone does not work because calling one particular number always returns a busy signal. You need to focus on the actual error rather than the idea that the micropython api is not working.