Unsupported Media Type, Backend, Please Guide Me
I am trying to receive a pdf file by using falcon framework as Backend. I am a beginner at backend and trying to understand what is happening. So summary, there are 2 classes. one
Solution 1:
Falcon has out of the box support for requests with Content-Type: application/json
.
For other content types, you need to provide a media handler for your request.
Here is an attempt at implementing a handler for requests of 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
classDocument(object):
def__init__(self, document):
self.document = document
# implement media methods hereclassPDFHandler(media.BaseHandler):
defserialize(self, media):
return media._parser.fp.getvalue()
defdeserialize(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)
)
Update media handlers to support 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)
Post a Comment for "Unsupported Media Type, Backend, Please Guide Me"