FlashAir SD card works with Firefox & curl, but not Python-Requests

365 Views Asked by At

I have a Toshiba FlashAir card, which is a wi-fi enabled SD card that apparently runs a webserver on embedded Linux.

I'm trying to upload files to it, which is through the standard multipart/form-data post, as per https://www.flashair-developers.com/en/documents/api/uploadcgi/

So it works with Firefox/Chrome/IE, and it works with curl:

curl --form [email protected] http://flashair.local/upload.cgi

However, if I use Python requests, it doesn't work:

r=requests.post('http://flashair.local/upload.cgi',
    files={'files': open('mydatafile.gcode', 'rb')})

The device rejects it with an "NG" response, which is its standard "I give up" (it's not very bright)

I've pored over the results of sending the requests to http://httpbin.org/post, and they don't look materially different. I've also prepared the request and looked at the body and headers. They're pretty much the same headers, same payload, and the same request structure as the browsers or curl.

Other things work fine:

requests.get('http://flashair.local/upload.cgi?WRITEPROTECT=ON')

requests.get('http://flashair.local/upload.cgi?UPDIR=/')

Does anyone see what I'm missing? Or can suggest some debugging tricks?

Edit: I figured it out finally. It turns out you have to explicitly specify the filename and a mime type of "text/plain"

r=requests.post('http://flashair.local/upload.cgi', files={'file': (file, open(file, 'rb'), 'text/plain')})

I don't have to do that for other webservers like Apache, but this is a very simple server.

0

There are 0 best solutions below