0

I am trying to send multiple file uploads to a function app (http trigger). The http trigger code is below,

    app = func.FunctionApp(http_auth_level=func.AuthLevel.ADMIN)
    @app.route(route="test_http_trigger", auth_level=func.AuthLevel.ADMIN)
    def test_http_trigger(req: func.HttpRequest) -> func.HttpResponse:
         try:
             if list(req.files.values())[0].filename.split('.')[-1] == 'zip':
                 logging.info(req.files.values())
                 return func.HttpResponse(str(req.files.values()), mimetype="text/html")
             elif list(req.files.values())[0].filename.split('.')[-1] == 'txt':
                 logging.info(req.files.values())
                 return func.HttpResponse(str(req.files.values()),  status_code=200, mimetype="application/json")
             else:
                 return func.HttpResponse('0')
         except Exception as e:
             return func.HttpResponse(f"Internal server error: {e}", status_code=500)

I tried to send multiple files as input using the curl command below,

curl -X POST http://localhost:7071/api/test_http_trigger -F "[email protected]" -F "[email protected]"

This only processes the first file sent. I would like to know the right way to send multiple files to function app.

Thanks,

1
  • Are you getting any error? Commented Jan 21 at 3:31

1 Answer 1

0

I have used the below code to process multiple files in Azure function.

import azure.functions as func
import logging

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    try:
        files = req.files  
        if not files:
            return func.HttpResponse("No files uploaded", status_code=400)
        
        responses = []
        for file_name, file in files.items():
            file_extension = file.filename.split('.')[-1].lower()
            logging.info(f"Processing file: {file.filename}")
            
            if file_extension == 'zip':
                responses.append(f"Processed ZIP file: {file.filename}")
            elif file_extension == 'txt':
                responses.append(f"Processed TXT file: {file.filename}")
            else:
                responses.append(f"Unsupported file type: {file.filename}")

        return func.HttpResponse("\n".join(responses), mimetype="text/plain", status_code=200)
    except Exception as e:
        logging.error(f"Error processing files: {e}")
        return func.HttpResponse(f"Internal server error: {e}", status_code=500)

I am getting expected response.

Azure Functions Core Tools
Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875

[2025-01-21T05:37:07.821Z] 0.01s - Debugger warning: It seems that frozen modules are being used, which may
[2025-01-21T05:37:07.823Z] 0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
[2025-01-21T05:37:07.824Z] 0.00s - to python to disable frozen modules.
[2025-01-21T05:37:07.825Z] 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.  
[2025-01-21T05:37:08.046Z] Worker process started and initialized.

Functions:

        http_trigger:  http://localhost:7071/api/http_trigger

For detailed output, run func with --verbose flag.
[2025-01-21T05:37:12.840Z] Host lock lease acquired by instance ID '0000000000000000000000000D2022A4'.
[2025-01-21T05:37:16.692Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=54612ce8-ac41-4db0-9536-12e3d9ff01e4)
[2025-01-21T05:37:22.382Z] Processing file: file1.zip
[2025-01-21T05:37:22.382Z] Processing file: file2.txt
[2025-01-21T05:37:22.472Z] Executed 'Functions.http_trigger' (Succeeded, Id=54612ce8-ac41-4db0-9536-12e3d9ff01e4, Duration=6744ms)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am using the exact script but it still processes only the first file. Did you also use CURL to call the function?
Yes, I have used curl -X POST http://localhost:7071/api/http_trigger -F "file1=@C:\Users\***\Downloads\file1.zip" -F "file2=@C:\Users\***\Downloads\file2.txt" command to trigger the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.