I’ve been using the Data API for a while now with a microcontroller (Raspberry Pi Pico W) and have this procedure embedded into various tutorials (e.g., 5. Logging Data — ac-microcourses 0.0.post1.dev51+gf0a96a7 documentation), videos, and a manuscript. However, recently the response has changed to (426) “Upgrade Required”. I spent a while trying to figure out if I needed to upgrade the Data API or upgrade the MongoDB Atlas version, and then settled on debugging by creating a new organization, new project, new cluster, etc. Finally, I realized I could run it in regular Python, but not in MicroPython. This appears to be due to the use of HTTP 1.0 protocol instead of HTTP 1.1 or higher [1] [2]. I found the MongoDB docs (Service Limitations), which states:
Data API and HTTPS Endpoints require HTTP/1.1 or greater when making requests.
I’m guessing this changed recently? (Since this has worked fine for me for at least a year)
import requests
import json
url = "https://us-east-2.aws.data.mongodb-api.com/app/data-<APP_ID>/endpoint/data/v1/action/insertOne"
payload = json.dumps({
"collection": "write-to-me",
"database": "test-db",
"dataSource": "Cluster0",
"projection": {
"_id": 1
}
})
headers = {
'Content-Type': 'application/json',
'Access-Control-Request-Headers': '*',
'api-key': '<API_KEY>,
}
response = urequests.request("POST", url, headers=headers, data=payload)
print(response.text)
I’ve been looking into HTTP 1.1 support from the microcontroller side, but it’s not looking so promising. Open to any thoughts or suggestions here.