3

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?

Rotareti
  • 49,483
  • 23
  • 112
  • 108
bzupnick
  • 2,646
  • 4
  • 25
  • 34

2 Answers2

5

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())}
0

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.

Liam Kelly
  • 3,524
  • 1
  • 17
  • 41