aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/request.py
diff options
context:
space:
mode:
Diffstat (limited to 'djangorestframework/request.py')
-rw-r--r--djangorestframework/request.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/djangorestframework/request.py b/djangorestframework/request.py
index c4381bbf..c5d98286 100644
--- a/djangorestframework/request.py
+++ b/djangorestframework/request.py
@@ -145,6 +145,39 @@ class RequestMixin(object):
self._stream = StringIO(content[self.CONTENT_PARAM])
del(self._raw_content)
+ def parse(self, stream, content_type):
+ """
+ Parse the request content.
+
+ May raise a 415 ResponseException (Unsupported Media Type),
+ or a 400 ResponseException (Bad Request).
+ """
+ parsers = as_tuple(self.parsers)
+
+ parser = None
+ for parser_cls in parsers:
+ if parser_cls.handles(content_type):
+ parser = parser_cls(self)
+ break
+
+ if parser is None:
+ raise ResponseException(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
+ {'error': 'Unsupported media type in request \'%s\'.' %
+ content_type.media_type})
+
+ return parser.parse(stream)
+
+ @property
+ def parsed_media_types(self):
+ """Return an list of all the media types that this view can parse."""
+ return [parser.media_type for parser in self.parsers]
+
+ @property
+ def default_parser(self):
+ """Return the view's most preffered emitter.
+ (This has no behavioural effect, but is may be used by documenting emitters)"""
+ return self.parsers[0]
+
method = property(_get_method, _set_method)
content_type = property(_get_content_type, _set_content_type)
accept = property(_get_accept, _set_accept)