3

有没有办法在拥抱中传递一个带有斜杠的字符串,例如使用这个函数:

import hug

@hug.get("/returnfilecontent/{path}")
def doubles(path):
    return open(path, 'r').read()

我想访问以http://localhost/returnfilecontent/foo/bar/myfile.md从位于foo/bar/myfile.md.

似乎拥抱在路径上表现不佳,我只能传递非路径字符串,例如http://localhost/returnfilecontent/myfile.md

4

1 回答 1

1

我不确定这是否是您正在寻找的,可能会有所帮助

import hug

@hug.get("/returnfilecontent")
def doubles(request, path: hug.types.text):
    return open(path, 'r').read()

您可以通过以下方式调用此获取请求

curl http://localhost:8000/returnfilecontent/\?path\=foo\/bar\/myfile.md

或者您可以尝试将它们作为 foo_bar_myfile.md 传递并拆分并加入它以使其成为路径

或者像这样

import hug

@hug.get("/returnfilecontent/{base_path}/{middle_folder}/{filename}")
def doubles(request, base_path: hug.types.text, middle_folder, filename):
    return f"{base_path}/{middle_folder}/{filename}"
于 2019-12-17T10:34:34.440 回答