【问题标题】:How to upload a file to Hug REST API如何将文件上传到 Hug REST API
【发布时间】:2016-08-24 06:42:22
【问题描述】:

我正在开发一个基本的 Hug API,我的一个函数需要一个文件。

Hug 有上传文件的方法吗?

【问题讨论】:

    标签: python python-3.x hug


    【解决方案1】:

    这个例子就是你要找的: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())}
    

    【讨论】:

      【解决方案2】:

      认为可以。查看input_format.py,您应该能够提取编码了一些法典(url、utf-8 等)的文件。看github readme,有这个例子:

      @hug.default_input_format("application/json")
      def my_input_formatter(data):
          return ('Results', hug.input_format.json(data))
      

      如果文件是 json 格式,那么您将从 json 对象中提取编码文件,将其转换为字节,然后将字节写入本地文件。

      【讨论】:

        猜你喜欢
        • 2019-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-05
        • 2016-07-21
        • 1970-01-01
        • 2016-05-06
        • 2017-08-28
        相关资源
        最近更新 更多