【问题标题】:Unsupported Media type, backend, Please guide me不支持的媒体类型,后端,请指导我
【发布时间】:2017-09-11 17:04:58
【问题描述】:

我正在尝试使用 falcon 框架作为后端来接收 pdf 文件。 我是后端的初学者,并试图了解正在发生的事情。所以总结一下,有2个类。其中之一是我正在工作的朋友。

这是后端代码:

#this is my code
class VehiclePolicyResource(object):
    def on_post(self, req, resp, reg):
        local_path = create_local_path(req.url, req.content_type)
        with open(local_path, 'wb') as temp_file:
            body = req.stream.read()
            temp_file.write(body)
#this is my friend code
class VehicleOdometerResource(object):
    def on_post(self, req, resp, reg):
        local_path = create_local_path(req.url, req.content_type)
        with open(local_path, 'wb') as temp_file:
            body = req.stream.read()
            temp_file.write(body)

完全一样,没有给出相同的答案,我通过这样做添加了路线 api.add_route('/v1/files/{reg}/policies',VehicleResourcesV1.VehiclePolicyResource())

并在终端中使用此命令: HTTP POST localhost:5000/v1/files/SJQ52883Y/policies@/Users/alfreddatui/Autoarmour/aa-atlas/static/asd.pdf 它试图获取文件。但它一直说,不支持的媒体类型。 而其他代码,接收图像,与上面的代码相同,它可以工作。

有什么想法吗?

【问题讨论】:

  • 抱歉命令错误:HTTP POST localhost:5000/v1/files/SJQ9957Y/policies @/Users/alfreddatui/Autoarmour/aa-atlas/static/asd.pdf
  • create_local_path 中发生了什么?当传递的内容类型不符合要求时是否引发异常
  • 哎呀抱歉,它创建了一个指向目录的字符串(我想将文件保存在该目录)这是代码:def create_local_path(url, content_type): image_type = url.split('/')[-1] ext = mimetypes.guess_extension(content_type) name = '{}/{}{}'.format(image_type, uuid.uuid4(), ext) return os.path.join('./temp/', name)
  • 我觉得很好。
  • 是的,我也是,看起来不错,但不起作用:(

标签: python backend falconframework


【解决方案1】:

Falcon 对Content-Type: application/json 的请求提供开箱即用的支持。

对于其他内容类型,您需要为您的请求提供媒体处理程序。

这是为Content-Type: application/pdf 的请求实现处理程序的尝试。

import cStringIO
import mimetypes
import uuid
import os

import falcon
from falcon import media
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument

class Document(object):
    def __init__(self, document):
        self.document = document
    # implement media methods here

class PDFHandler(media.BaseHandler):
    def serialize(self, media):
        return media._parser.fp.getvalue()

    def deserialize(self, raw):
        fp = cStringIO.StringIO()
        fp.write(raw)
        try:
            return Document(
                PDFDocument(
                    PDFParser(fp)
                )
            )
        except ValueError as err:
            raise errors.HTTPBadRequest(
                'Invalid PDF',
                'Could not parse PDF body - {0}'.format(err)
            )

更新媒体处理程序以支持Content-Type: application/pdf

extra_handlers = {
    'application/pdf': PDFHandler(),
}

app = falcon.API()
app.req_options.media_handlers.update(extra_handlers)
app.resp_options.media_handlers.update(extra_handlers)

【讨论】:

  • 发现这个解决方案非常有用,非常感谢。需要对 python3.x 做一些更改。
【解决方案2】:

我明白了, 我只是注意到 Falcon 默认会收到 JSON 文件(如果我错了请纠正我) 所以我需要对pdf和图像文件进行例外处理。

【讨论】:

  • 您对application/json的请求内容类型的默认处理程序是正确的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 2019-08-02
  • 1970-01-01
相关资源
最近更新 更多