litestar-org / starlite-multipart

Toolkit for working with multipart/formdata
https://starlite-api.github.io/starlite-multipart/
MIT License
8 stars 2 forks source link

Error when uploading photo using UploadFile #3

Closed devmitch closed 2 years ago

devmitch commented 2 years ago

Minimal reproducible example on this repo.

When posting a photo to a route with an UploadFile parameter, an exception is thrown:

@post("/")
async def hello_world(
    data: UploadFile = Body(media_type=RequestEncodingType.MULTI_PART),
) -> dict[str, Any]:
    form_data = await data.read()
    print(len(form_data))
    return {"hello": "world"}
with open("flower.jpg", "rb") as f:
    data = f.read()

with httpx.Client() as client:
    client.post("http://localhost:8000/", files={"data": data})
Traceback (most recent call last):
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/middleware/exceptions.py", line 49, in __call__
    await self.app(scope, receive, send)
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/routes/http.py", line 72, in handle
    response = await self._get_response_for_request(
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/routes/http.py", line 116, in _get_response_for_request
    response = await self._call_handler_function(
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/routes/http.py", line 144, in _call_handler_function
    response_data = await self._get_response_data(
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/routes/http.py", line 168, in _get_response_data
    kwargs["data"] = await request_data
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/kwargs.py", line 540, in _get_request_data
    form_data = await request.form()
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite/connection/request.py", line 140, in form
    form_values = await parser()
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite_multipart/parser.py", line 84, in __call__
    parse_result.extend(await self._parse_chunk())
  File "/home/mitch/uni/starlite-multipart-error/.venv/lib/python3.9/site-packages/starlite_multipart/parser.py", line 65, in _parse_chunk
    items.append((field_name, data.decode(self.charset)))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 2: invalid start byte
Goldziher commented 2 years ago

Please confirm if this issue is resolved @devmitch

devmitch commented 2 years ago

All fixed, thanks @Goldziher @jtraub