【问题标题】:Passing path like string in hug server在拥抱服务器中像字符串一样传递路径
【发布时间】:2019-12-08 09:20:45
【问题描述】:

有没有什么方法可以传递一个带有斜线的字符串,例如这个函数:

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 的文件中读取内容。

似乎拥抱不behave well 带有路径,我只能传递像http://localhost/returnfilecontent/myfile.md 这样的非路径字符串

【问题讨论】:

    标签: routing hug


    【解决方案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}"
    

    【讨论】:

    • 嗯。这不正是我想要的。我想我想用this 创建一个类似路径的字符串
    • ?path\=foo\/bar\/myfile.md 对我不起作用。请验证答案。
    • 我为此使用了 curl。你可以得到你发送的任何东西作为函数内部的路径
    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多