Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

fastapi extensions support multipart data #1563

Closed Anton-Buzik closed 2 months ago

Anton-Buzik commented 2 months ago

Azure Functions runtime version 4.34.1, or a later version. Python version 3.9, or a later Python v2 programming model introduction post for the preview

Does the fastapi extensions request support multipart data? i would like to provide some files with the request and stream the response. But when loading the form-data its always empty. Is it not yet supported or is there another way to get the form-data?

@app.route(route="stream_files_resp", methods=[func.HttpMethod.POST])
async def func_stream_files_resp(
    request: Request
) -> StreamingResponse:
    form_data = await request.form()
    return StreamingResponse(some_generator(form_data), media_type="text/event-stream")
YunchuWang commented 2 months ago

thanks for raising! i am able to upload multipart data and retrieve it in the function. have you installed python-multipart package in requirements.txt? also can you show the http request message? does it set header: Content-Type: multipart/form-data; boundary=XXX with right boundary?

image

Anton-Buzik commented 2 months ago

I already have a few functions that also contain some code. That is why I have only registered the functions in the function_app.py and left them in their own files. apparently it is not possible to pass the whole request to the functions, information is lost.

function_app.py

@app.route(route="stream_files_resp", methods=[func.HttpMethod.POST])
async def func_stream_files_resp(
    request: Request
) -> StreamingResponse:
    return StreamingResponse(some_generator_function(request), media_type="text/event-stream")

some_generator.py

def some_generator_function(req: Request):
    form_data = await req.form()
    request_json = form_data.get("json")
    request_file = form_data.get("file")
    cookies = get_cookies(req.headers.get("cookie"))

in the case above the formdata was always empty.

changed now to the following and it works like a charm. function_app.py

@app.route(route="stream_files_resp", methods=[func.HttpMethod.POST])
async def func_stream_files_resp(
    request: Request
) -> StreamingResponse:
    form_data = await req.form()
    request_json = form_data.get("json")
    request_file = form_data.get("file")
    return StreamingResponse(some_generator_function(request_json, request_file), media_type="text/event-stream")

some_generator.py

def some_generator_function(request_json, request_file):
   *some code*

Sorry for the confusion and thank you very much

Anton-Buzik commented 2 months ago

Everything is resolved.