def prefer_json(self): for d in accept.parse(self.request.headers.get('Accept')): if d.media_type == 'application/json': return True elif d.media_type == 'text/html' or d.all_types: return False return False
def __choose_encoder(self, request): for accept_header in accept.parse(request.headers.get('Accept')): if accept_header.media_type == '*/*': return self.__encoders.get(self.DEFAULT_CONTENT_TYPE) encoder = self.__encoders.get(accept_header.media_type) if encoder is not None: return encoder raise web_exceptions.HTTPNotAcceptable()
def prepare(self): if len(self.request.body) != 0: mt = accept.parse(self.request.headers.get("Content-Type"))[0] if mt.media_type != self._get_content_type( ) or not self.acceptable(mt.params): raise APIError(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, "") accept_header = self.request.headers.get("Accept") if not accept_header: return # allow missing Accept header for a in accept.parse(accept_header): if (a.all_types or a.media_type.split("/")[0] == "application" and a.all_subtypes): return if a.media_type == self._get_content_type(): if self.acceptable(a.params): return raise APIError(status.HTTP_406_NOT_ACCEPTABLE, "")
def prefer_json(self): accept_header = self.request.headers.get('Accept') try: parse_result = accept.parse(accept_header) except (ValueError, IndexError): # the accept library throws bogus exceptions _logger.warning('Unparsable accept header: %s', accept_header) return False for d in parse_result: if d.media_type == 'application/json': return True elif d.media_type == 'text/html' or d.all_types: return False return False