3

Is there any way to pass a string with slashes in hug, for example with this function:

import hug

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

I want to access to http://localhost/returnfilecontent/foo/bar/myfile.md to read contents from file located in foo/bar/myfile.md.

It seems hug does not behave well with paths and I can only pass non-path strings like http://localhost/returnfilecontent/myfile.md

somenxavier
  • 1,206
  • 3
  • 20
  • 43

1 Answers1

1

I'm not sure whether this is what you are looking for, May be this helps

import hug

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

You can call this get request by

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

Or you can try passing in those as foo_bar_myfile.md and split and join it to make it a path

or like this

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}"
Akhil Suresh
  • 151
  • 9