Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If request.body is a file-like object, send fails #38

Open
MerlijnWajer opened this issue Jul 23, 2023 · 6 comments
Open

If request.body is a file-like object, send fails #38

MerlijnWajer opened this issue Jul 23, 2023 · 6 comments

Comments

@MerlijnWajer
Copy link

MerlijnWajer commented Jul 23, 2023

When using the python requests library, if one sets the body (data) to a file-like object, such as io.BytesIO or io.StringIO, the body becomes something like <_io.BytesIO object at 0x1550d50> when using pyodide-http, as opposed to the actual contents of the file-like object.

Below is a very hacky fix for this problem:

diff --git a/pyodide_http/_core.py b/pyodide_http/_core.py
index 1cb711d..e8de6b9 100644
--- a/pyodide_http/_core.py
+++ b/pyodide_http/_core.py
@@ -118,7 +118,13 @@ def send(request: Request, stream: bool = False) -> Response:
         if name.lower() not in HEADERS_TO_IGNORE:
             xhr.setRequestHeader(name, value)

-    xhr.send(to_js(request.body))
+    body = request.body
+
+    if hasattr(body, 'read'):
+        body = body.read()
+
+    xhr.send(to_js(body))
+    #xhr.send(to_js(request.body))

     headers = dict(Parser().parsestr(xhr.getAllResponseHeaders()))

I suppose that perhaps a stream method could be used with a file-like object? Any ideas on how to best solve this problem?

@MerlijnWajer
Copy link
Author

(I ran into this when using the internetarchive library - as discussed here jjjake/internetarchive#550)

Otherwise the library mostly seems to work, so that's great.

@MerlijnWajer
Copy link
Author

@koenvo - do you have any opinion on what is the right fix for this?

@koenvo
Copy link
Owner

koenvo commented Aug 1, 2023

@koenvo - do you have any opinion on what is the right fix for this?

Hi! Sorry for the late reply.

As your solution follows the behavior of requests (because it works), it looks like a good solution for now.

When people like to do streaming uploads another solution is required. But at this moment there doesn’t seem to be a blocking streaming solution possible. XMLHttpRequest can’t do streaming upload and fetch isn’t blocking.

Are you able and willing to create a PR for this?

@MerlijnWajer
Copy link
Author

Sure thing, I'll do that today, thanks for the reply.

@koenvo
Copy link
Owner

koenvo commented Sep 3, 2023

hi @MerlijnWajer, do you need any help with this?

MerlijnWajer added a commit to MerlijnWajer/pyodide-http that referenced this issue Dec 11, 2023
If request.body is a file(-like) interface, read the full contents and
then send it.

See koenvo#38
@MerlijnWajer
Copy link
Author

Just made the pull request, sorry for the extreme delay, I was very busy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants