2

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

Ayan Mullick
  • 121
  • 1
  • 5

2 Answers2

1

I found a working example of a get request using the pi pico Wokwi simulator here:

https://wokwi.com/projects/386002857005046785

It looks like there might be something wrong with that specific GET call you're using that's getting it blocked. Not sure if it helps, but maybe you can work backward from the working example.

Charlie
  • 11
  • 1
0

I had the same issue and it turned out to be a lack of RAM that made the request end prematurely. I solved it by letting Garbage Collection run just before the request.

# At your imports
import gc

Before your request

gc.collect()

#Your request and the rest of the code response = urequests.get('https://official-joke-api.appspot.com/random_joke')

Love Dager
  • 101
  • 1