koenvo / pyodide-http

Provides patches for widely used http libraries to make them work in Pyodide environments like JupyterLite
MIT License
78 stars 15 forks source link

XMLHttpRequest.responseType async #2

Closed 0xMochan closed 2 years ago

0xMochan commented 2 years ago

According to the docs, setting responseType on the XMLHttpRequest object sets the requirement that the call must be async.

This results in the following exception in pyscript.

xhr.open(request.method, request.url, False)

JsException: InvalidAccessError: Failed to execute 'open' on 'XMLHttpRequest': Synchronous requests from a document must not set a response type. )
koenvo commented 2 years ago

Thanks for you PR.

That's interesting problem.

I think there is a distinction needed between JupyterLite and pyscript. In JupyterLite the request is executed from a Worker. From the docs: "You cannot change the value of responseType in a synchronous XMLHttpRequest except when the request belongs to a Worker."

Did you test your solution with binary files? Like images or zip files?

0xMochan commented 2 years ago

Yea for sure, detecting if you are in a web-worker will be important. I have not, def. a worthwhile test since I highly doubt json.loads will work on those file formats. However, there's also the XMLHttpRequest.open_url pyodide.http.open_url which looks better suited for those types? Ref: https://pyodide.org/en/stable/usage/api/python-api/http.html?highlight=fetch

koenvo commented 2 years ago

I made some changes. Can you check version 0.0.8 ? The fix is based on your PR + https://stackoverflow.com/questions/44974003/recover-arraybuffer-from-xhr-responsetext

This makes it also possible to read data with pandas.

image