Python 不支持的媒体类型,后端,请指导我
我试图通过使用falcon框架作为后端来接收pdf文件。 我是后端的初学者,试图了解正在发生的事情。总之,有两个类。其中一个,是我正在工作的朋友 这是后端端代码:Python 不支持的媒体类型,后端,请指导我,python,backend,falconframework,Python,Backend,Falconframework,我试图通过使用falcon框架作为后端来接收pdf文件。 我是后端的初学者,试图了解正在发生的事情。总之,有两个类。其中一个,是我正在工作的朋友 这是后端端代码: #this is my code class VehiclePolicyResource(object): def on_post(self, req, resp, reg): local_path = create_local_path(req.url, req.content_type) w
#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',VehiclerResourcesV1.VehiclePolicyResource())
通过在终端中使用此命令:
HTTP POST localhost:5000/v1/files/SJQ52883Y/policies@/Users/alfreddatui/autoarmor/aa atlas/static/asd.pdf
它试图获取文件。但它一直在说,不支持的媒体类型。
而另一个代码,接收图像,字面上与上面的代码相同,它可以工作
有什么想法吗?我知道了,
我只是注意到Falcon默认将接收JSON文件(如果我错了,请纠正我)
因此,我需要对pdf和图像文件做一个例外。Falcon对
内容类型为application/json的请求提供现成的支持
对于其他内容类型,您需要为您的请求提供媒体处理程序
下面尝试为内容类型为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)
)
extra_handlers = {
'application/pdf': PDFHandler(),
}
app = falcon.API()
app.req_options.media_handlers.update(extra_handlers)
app.resp_options.media_handlers.update(extra_handlers)
更新媒体处理程序以支持内容类型: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)
)
extra_handlers = {
'application/pdf': PDFHandler(),
}
app = falcon.API()
app.req_options.media_handlers.update(extra_handlers)
app.resp_options.media_handlers.update(extra_handlers)
抱歉,错误的命令:HTTP POST localhost:5000/v1/files/SJQ9957Y/policies@/Users/alfreddatui/autoarmer/aa atlas/static/asd.pdf在创建本地路径中发生了什么?当传递的内容类型不适合billoops时是否会引发异常抱歉,它会生成一个指向目录(我想将文件保存在)的字符串这是代码:def create_local_path(url,content_type):image_type=url.split('/')[-1]ext=mimetypes.guess_extension(content_type)name='{}/{}}。格式(image_type,uuid.uuid4(),ext)return os.path.join('./temp/',name)
在我看来很好。是的,我也很好,但不起作用:(关于应用程序/json