How to pass an array of strings to fastapi in python
Question:
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.
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.
You may use query-alias like this to parse a list of values:
def get_entries(lst_of_entries: list | None = Query(alias="lst_of_entries[]", default=None)):
print(lst_of_entries, type(lst_of_entries))
Outputs:
['value1', 'value2'] <class 'list'>
On the client side values are sent like this:
?lst_of_entries[]=value1&lst_of_entries[]=value2
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.
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.
You may use query-alias like this to parse a list of values:
def get_entries(lst_of_entries: list | None = Query(alias="lst_of_entries[]", default=None)):
print(lst_of_entries, type(lst_of_entries))
Outputs:
['value1', 'value2'] <class 'list'>
On the client side values are sent like this:
?lst_of_entries[]=value1&lst_of_entries[]=value2