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

6
  • 2
    Could be the client is rejecting the server certificate: github.com/espressif/esp-idf/issues/2907 Note that your title implies you can't get any API call to work, which is obviously not true since the ones prior to 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.
    – goldilocks
    Commented May 10, 2023 at 13:04
  • I realize you are a new user, Ayan, please keep it clean in the future. WRT your error, again, you need to focus on the specifics.
    – goldilocks
    Commented May 11, 2023 at 14:31
  • I realize you are unable to see the specifics I commented yesterday. I am getting errors on other URL's too on the Pico only and that they were working elsewhere. Kindly coordinate with moderators and focus on the question in the future. Commented May 11, 2023 at 14:57
  • I can still read the deleted comment. Think about your problem solving strategy here: Certainly someone else would have noticed by now if SSL did not work on the Pico W. So it is some more specific detail WRT SSL authentication. You might want to have a look at this: github.com/micropython/micropython/issues/9071 If you want help here, you probably need to go into details about how you have provided CA certs etc. on the Pico, otherwise most people will look at this and just think, "Oh, he doesn't understand SSL, that's an authentication failure or some such thing..."
    – goldilocks
    Commented May 11, 2023 at 15:04
  • I was getting an HTTP error, not SSL; yesterday with the other links. And only on the Pico. Commented May 11, 2023 at 15:26

2 Answers 2

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.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 9 at 18:38
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')
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.