async def form(self) -> FormData: if self.content_type == "multipart/form-data": multipart_parser = MultiPartParser(self.headers, self.stream()) return await multipart_parser.parse() if self.content_type == "application/x-www-form-urlencoded": form_parser = FormParser(self.headers, self.stream()) return await form_parser.parse() return FormData()
async def parse_multipart_form(headers: "Headers", body: bytes) -> "FormData": """ parse starlette forms from headers and body """ from starlette.formparsers import MultiPartParser async def async_bytesio(bytes_: bytes) -> t.AsyncGenerator[bytes, None]: yield bytes_ yield b"" return parser = MultiPartParser(headers=headers, stream=async_bytesio(body)) return await parser.parse()
async def form(self) -> FormData: if not hasattr(self, "_form"): assert ( parse_options_header is not None ), "The `python-multipart` library must be installed to use form parsing." content_type_header = self.headers.get("Content-Type") content_type, options = parse_options_header(content_type_header) if content_type == b"multipart/form-data": multipart_parser = MultiPartParser(self.headers, self.stream()) self._form = await multipart_parser.parse() elif content_type == b"application/x-www-form-urlencoded": form_parser = FormParser(self.headers, self.stream()) self._form = await form_parser.parse() else: self._form = FormData() return self._form
async def form(self) -> FormData: if not hasattr(self, "_form"): assert ( parse_options_header is not None ), "The `python-multipart` library must be installed to use form parsing." content_type_header = self.headers.get("Content-Type") content_type: bytes content_type, _ = parse_options_header(content_type_header) if content_type == b"multipart/form-data": try: multipart_parser = MultiPartParser(self.headers, self.stream()) self._form = await multipart_parser.parse() except MultiPartException as exc: if "app" in self.scope: raise HTTPException(status_code=400, detail=exc.message) raise exc elif content_type == b"application/x-www-form-urlencoded": form_parser = FormParser(self.headers, self.stream()) self._form = await form_parser.parse() else: self._form = FormData() return self._form