1

I have uploaded a .mp4 file in postman and now i need to read the file contents into a variable in python This is my postman upload enter image description here

This is my python file where i my request will be catched in this function now how will be decode the file uploaded in this function

from starlette.requests import Request
from starlette.routing import Route, Mount
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse, FileResponse   

class Post:
    async def PostValuesToDb(requests: Request):
        return PlainTextResponse("true")


routes = [
    Route("/post", Post.PostValuesToDb,
          name="PostValuesToDb", methods=["POST"]),

]

app = Starlette(
    routes=routes
)

PostValuesToDb(requests: Request) captures the postman request.

SPMK
  • 19
  • 5

1 Answers1

0

as per the documentation

for the time being i am choosing to save the contents in a local directory, you can do as you wish from the contents variable

from starlette.requests import Request
from starlette.routing import Route, Mount
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse, FileResponse   

class Post:
    async def PostValuesToDb(requests: Request):
        form = await requests.form()
        # print(dir(form)) # check what methods and attrs are available
        # print(form.multi_items) # check all form fields submitted
        filename = form["image"].filename # my particular form field
        contents = await form["image"].read()
        with open(f'./data/{filename}', 'wb') as uploaded_file:
            uploaded_file.write(contents)
        return PlainTextResponse("true")


routes = [
    Route("/post", Post.PostValuesToDb,
          name="PostValuesToDb", methods=["POST"]),

]

app = Starlette(
    routes=routes
)
Kapil
  • 165
  • 1
  • 9