I am trying to read a text file from Dropbox on a Raspberry Pi Pico with CircuitPython using adafruit_requests
.
The file should be available to view and download "for everybody who has the link". When I enter the url with ?raw=1
, it leads to a website containing only the string of text.
I connected via Wifi and ran
from adafruit_requests import get
response = get("https://www.dropbox.com/s/<content_ID>/file_name.txt?raw=1")
content = response.text
Now, when I ran
print(content)
I got the following output:
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Dropbox</title>
<link href="https://cfl.dropboxstatic.com/static/metaserver/static/css/error.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="https://cfl.dropboxstatic.com/static/metaserver/static/images/favicon.ico"/>
</head>
<body>
<div class="figure">
<img src="https://cfl.dropboxstatic.com/static/metaserver/static/images/illustration_catalog/sickbox-illo_m1.png" srcset="https://cfl.dropboxstatic.com/static/metaserver/static/images/illustration_catalog/sickbox-illo_m1@2x.png 2x" alt="Error: 5xx"/>
</div>
<div id="errorbox">
<h1>Error</h1>Something went wrong. Don't worry, your files are still safe and the Dropboxers have been notified. Check out our <a href="https://www.dropbox.com/help">Help Center</a> and <a href="https://forums.dropbox.com">forums</a> for help, or head back to <a href="https://www.dropbox.com/home">home</a>.
</div>
<script>
function read_cookie (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function localize() {
var locale = read_cookie('locale');
if (locale) {
var msg = message[locale];
if (msg) {
var elem = document.getElementById('errorbox');
if (elem) {
elem.innerHTML = msg;
}
}
}
}
localize();
</script>
</body>
</html>
Obviously, this is not the .txt file I uploaded to Dropbox, but an error message.
Is there a way to read the raw text file from Dropbox into a variable of type string in CircuitPython?
Note: When I use my Laptop and regular Python 3, I can easily obtain the string I want by executing
import requests
response = requests.get("https://www.dropbox.com/s/<content_ID>/file_name.txt?raw=1")
content = response.content
(Here, I am using response.content
which, in the CircuitPython example produces the same error message as response.text
but encoded differently. The two modules are not identical in their output.)