Skip to content

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

Description

@MerlijnWajer

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions