fastapi a simple function greet(*args) sdf

106 Views Asked by At

I have simple function that takes an arbitrary number of arguments like so:

def greet(*args):
    a=list(args)
    return {"greetings to  users:": a}

greet('Aron','Claus')
>>>{'greetings to  users:': ['Aron', 'Claus']}

The function works as expected. But when i put a router decorator on the function like so:

@router.get("/greet")
def greet(*args):
    a=list(args)
    return {"greetings to  users:": a}

I get an internal server error on swagger side and my commandline gives me the following error:

TypeError: greet() got an unexpected keyword argument 'args'

Why is this happening how can I avoid this error. Thanks in advance

1

There are 1 best solutions below

0
On BEST ANSWER

So what i found is the following from the Fastapi documentation

from typing import List, Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[List[str], None] = Query(default=None)):
    query_items = {"q": q}
    return query_items

The URL would be like: http://localhost:8000/items/?q=foo&q=bar

It works fine.