I'm working on a basic Hug API and one of my functions needs a file.
Does Hug have a way to upload a file?
I'm working on a basic Hug API and one of my functions needs a file.
Does Hug have a way to upload a file?
This example is what you're looking for: https://github.com/timothycrosley/hug/blob/develop/examples/file_upload_example.py
@hug.post('/upload')
def upload_file(body):
"""accepts file uploads"""
# is a simple dictionary of {filename: b'content'}
print('body: ', body)
return {'filename': list(body.keys()).pop(), 'filesize': len(list(body.values()).pop())}
I think it can. Looking at the input_format.py you should be able to extract a file encoded some codex (url, utf-8, etc). Looking at the github readme, there is this example:
@hug.default_input_format("application/json")
def my_input_formatter(data):
return ('Results', hug.input_format.json(data))
If the file was in json format, then you would extract the encoded file from the json object, convert it to bytes, then write the bytes to a local file.