Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an array of strings to fastapi in python

Here is the function I made:

@app.get("/shows/")
def get_items(q: List[str] = Query(None)):
    '''
    Pass path to function.
    Returns folders and files.
    '''
    results = {}

    query_items = {"q": q}
    if query_items["q"]:
        entry = PATH + "/".join(query_items["q"])
    else:
        entry = PATH

    if os.path.isfile(entry):
        return download(entry)

    dirs = os.listdir(entry + "/")
    results["folders"] = [
        val for val in dirs if os.path.isdir(entry + "/" + val)]
    results["files"] = [val for val in dirs if
                        os.path.isfile(entry + "/" + val)]
    results["path_vars"] = query_items["q"]

    return results

Idea is to pass an array of string which essentially form a path in the function to a file and I can have some array to it from an app to serve files and send this function an array of string as they traverse the folders. But.. I cant figure out how to send a list of params from something like python requests.

Here is a sample function I wrote.

def try_url():
    url = "http://192.168.0.16:8000/shows/"

    payload = {
        "q": ["downloads",
              "showname"]
    }
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    print(response.text.encode('utf8'))

Api doesnt even accept a q value. What am I missing? Is this the right way to traverse dirs? In url format, this is what a request looks like:

http://192.168.0.16:8000/shows/?q=downloads&q=foldername

Doesnt look right to me.

like image 721
ScipioAfricanus Avatar asked Dec 18 '25 07:12

ScipioAfricanus


1 Answers

I am dumb, the solution was simply to pass params to the call ...

payload = {
    "q" : ["downloads",
    "Brooklyn.Nine-Nine.S07E01.720p.HEVC.x265-MeGusta"]
}
headers = {
}

response = requests.request("GET", url, headers=headers, params = payload)

adding "params" in requests.request is the solution.

like image 96
ScipioAfricanus Avatar answered Dec 20 '25 20:12

ScipioAfricanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!