Giter Club home page Giter Club logo

starlite-multipart's Introduction

Starlite Multipart

Starlite logo

Discord Matrix

Quality Gate Status Coverage

Reliability Rating Bugs Technical Debt Security Rating

This library offers a toolkit for working with multipart form-data. It's based on the Werkzeug multipart decoder/encoder implementation and is designed to be a zero dependency library that can be used on its own.

Its also used internally by Starlite, but Starlite is not required for it to function.

Checkout the docs ๐Ÿ“š.

Installation

pip install starlite-multipart

starlite-multipart's People

Contributors

goldziher avatar jtraub avatar provinzkraut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

devmitch jtraub

starlite-multipart's Issues

Error when uploading photo using UploadFile

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

headers in UploadFile should be a case insensitive multidict

It is super inconvenient that UploadFile uses ordinary dict for headers.

As we discussed in litestar-org/litestar#574 header names are case insensitive so using dict in UploadFile makes user code super complicated because now the user needs to handle different spellings for headers or use lower case everywhere. Also, headers in requests in Starlite are returned in a CI multidict so consistent behavior across the framework is another reason for this change.

Starlite uses starlette.datastructures.Headers for request headers as can be seen here. Internally it just an implementation of case insensitive multidict.

Intuitive solution would be reusing starlette.datastructures.Headers but then it adds Starlette as dependency for starlite-multipart. Doing so while considering departing from Starlette foundation is strange though.

Should we implement our own CI multidict? If yes how should we share the code between this project and the framework?

Optional Multipart query parameter not working

Minimal reproducible examples here

It seems that pydantic model fields that have the Optional type work - but if the query parameter is optional itself, it doesn't work and fails with a validation error.

Any improvements on Werkzeug?

Hello,

I've done quite a bit of work on Werkzeug's multipart parser, so I recognize this code :). Are there improvements that can be incorporated back in to Werkzeug?

Have you considered Starlite depending on Werkzeug? What puts you off?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.