Connect to web/RP2040/Python/requests

I am trying to read the response object using Micropython/Thonny. I am using the requests library. I need to process https, not just http.

urequest doesn't work. SSL doesn't work. urllib, urllib2, urllib3 don't work.

When I use requests, the text attribute, I get the error

Traceback (most recent call last):
  File "<stdin>", line 22, in <module>
AttributeError: 'Response' object has no attribute 'text'

Here is the code:

import network,socket,requests
from machine import Pin

sta_if = network.WLAN(network.STA_IF)  
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(False) # deactivate the interface
gotthrough=False
if not sta_if.isconnected():
    sta_if.active(True)
    while not gotthrough:
      try:   
        print('connecting to network...')
        sta_if.connect('<SSID>', '<password>')  # ADAPT
        while not sta_if.isconnected():
          pass
        gotthrough=True  
      except:   
        gotthrough=False


theresponse=requests.get('https://cecilchua.online/arduino/arduinoregister.php?boardid=00-00-00-00-00-00')
print(theresponse.text)

theresponse.content is empty. A visit to the URL will reveal it generates text, so it should not be empty. theresponse.json() etc. likewise don't return anything.

I have tried creating my own client using import ssl.

Essentially same code, but remove the requests and import ssl. Then:

sl=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s=ssl.wrap_socket(sl)

I get:

Traceback (most recent call last):
  File "<stdin>", line 21, in <module>
  File "ssl.py", line 1, in wrap_socket
  File "ssl.py", line 1, in wrap_socket
OSError: (-537093200, 'MBEDTLS_ERR_UNKNOWN (0x6400)+MBEDTLS_ERR_UNKNOWN (0x20030050)')

Anyone know how I can fix this or move forward? All I want to do is use Micropython to connect to a HTTPS web page and read the content.

@aehchua when I did a search I got the following AI response (almost)

import network
import requests

# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

if not wlan.isconnected():
    print('Connecting to Wi-Fi...')
    wlan.connect(SSID,PASSWORD)
    while not wlan.isconnected():
        pass
print('Wi-Fi connected')

# HTTPS request
try:
    url='https://cecilchua.online/arduino/arduinoregister.php?boardid=00-00-00-00-00-00'
    response = requests.get(url) 
    print(response.text)
    response.close()

except Exception as e:
    print(f"Error: {e}")

finally:
    wlan.disconnect()
    wlan.active(False)
MPY: soft reboot
Connecting to Wi-Fi...
Wi-Fi connected
Error: 'Response' object has no attribute 'text'

@aehchua odd it works for me on an ESP32

When I try on my microcontroller I get the response "board already registered" with the url in your listing.

The AI generated code example had url='https://example.com' as the url to test with, try that and see if it is any better.

I have a feeling there might be a problem with the requests library.

EDIT
you could also try print(response.content) or print(response.json())

Same error with example.com

With JSON:

Traceback (most recent call last):
  File "<stdin>", line 21, in <module>
  File "requests.py", line 51, in json
ValueError: syntax error in JSON

response.content returns blank.

Yes, I suspect there's a problem with the requests RP2040 library. But I am not sure what to do. Like I said, urequests, urllib...3 etc. can't be imported and the ssl library gives me a strange error.

@aehchua OK sry that's the limit for me.

Hopefully you will have some luck with the issue soon.

EDIT one last thing you can do is print(dir(response)) which will give you the list of attributes of 'Response'

I think I've solved it.

requests seems to be borked. I don't know if ssl is borked. I was able to do this with tls.

import tls
,
,
,
    mycontext=tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
    mycontext.verify_mode=tls.CERT_NONE
    sl=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sl.connect(socket.getaddrinfo(server, port)[0][-1])   #need to connect to the server before wrapping socket in SSL 
    s=mycontext.wrap_socket(sl,server_side=False)