'FastAPI - dynamic inputs for different data types

I have to create a very generic endpoint for a user. The user may send JSON data or may upload a file to this endpoint.

There are multiple options on how to handle different input types, like Request or UploadFile

I thought about solving it like this (pseudo code):

from enum import Enum

class Multiple(Enum):
    request: Request
    file: UploadFile

@app.post("/")
async def root(Multiple):
    if Multiple == request:
        do xxx 
    else if Multiple == file:
        do yyyy
    else: 
        do zzz

Is there any way to make something like this work?



Solution 1:[1]

You could do that by checking the Content-Type in the request's headers before parsing the data. Regardless, make sure to handle exceptions, in case the given data is not valid JSON/Form/etc. Also, when uploading the file from the client, make sure to use the same "key" you define in the endpoint; below, that is, upload_file. Thus, the client should use, in Javascript for instance, formData.append("upload_file", fileInput.files[0]);

from json import JSONDecodeError
 
@app.post("/upload")
async def upload(request: Request):
    content_type = request.headers.get('Content-Type')
    if content_type is None:
        return 'No Content-Type provided!'
    elif content_type == "application/json":
        try:
            json = await request.json()
            return json
        except JSONDecodeError:
            return "Invalid JSON data"
    elif content_type.startswith("multipart/form-data"):
        try:
            form = await request.form()
            filename = form["upload_file"].filename
            contents = await form["upload_file"].read()
            return filename
        except Exception as e:
            return "Invalid Form data"
    else:
        return 'Content-Type not supported!'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1