aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide/parsers.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/api-guide/parsers.md')
-rw-r--r--docs/api-guide/parsers.md82
1 files changed, 57 insertions, 25 deletions
diff --git a/docs/api-guide/parsers.md b/docs/api-guide/parsers.md
index 185b616c..1030fcb6 100644
--- a/docs/api-guide/parsers.md
+++ b/docs/api-guide/parsers.md
@@ -14,6 +14,16 @@ REST framework includes a number of built in Parser classes, that allow you to a
The set of valid parsers for a view is always defined as a list of classes. When either `request.DATA` or `request.FILES` is accessed, REST framework will examine the `Content-Type` header on the incoming request, and determine which parser to use to parse the request content.
+---
+
+**Note**: When developing client applications always remember to make sure you're setting the `Content-Type` header when sending data in an HTTP request.
+
+If you don't set the content type, most clients will default to using `'application/x-www-form-urlencoded'`, which may not be what you wanted.
+
+As an example, if you are sending `json` encoded data using jQuery with the [.ajax() method][jquery-ajax], you should make sure to include the `contentType: 'application/json'` setting.
+
+---
+
## Setting the parsers
The default set of parsers may be set globally, using the `DEFAULT_PARSER_CLASSES` setting. For example, the following settings would allow requests with `YAML` content.
@@ -24,7 +34,12 @@ The default set of parsers may be set globally, using the `DEFAULT_PARSER_CLASSE
)
}
-You can also set the renderers used for an individual view, using the `APIView` class based views.
+You can also set the parsers used for an individual view, or viewset,
+using the `APIView` class based views.
+
+ from rest_framework.parsers import YAMLParser
+ from rest_framework.response import Response
+ from rest_framework.views import APIView
class ExampleView(APIView):
"""
@@ -59,6 +74,8 @@ Parses `JSON` request content.
Parses `YAML` request content.
+Requires the `pyyaml` package to be installed.
+
**.media_type**: `application/yaml`
## XMLParser
@@ -69,6 +86,8 @@ Note that the `XML` markup language is typically used as the base language for m
If you are considering using `XML` for your API, you may want to consider implementing a custom renderer and parser for your specific requirements, and using an existing domain-specific media-type, or creating your own custom XML-based media-type.
+Requires the `defusedxml` package to be installed.
+
**.media_type**: `application/xml`
## FormParser
@@ -87,6 +106,33 @@ You will typically want to use both `FormParser` and `MultiPartParser` together
**.media_type**: `multipart/form-data`
+## FileUploadParser
+
+Parses raw file upload content. The `request.DATA` property will be an empty `QueryDict`, and `request.FILES` will be a dictionary with a single key `'file'` containing the uploaded file.
+
+If the view used with `FileUploadParser` is called with a `filename` URL keyword argument, then that argument will be used as the filename. If it is called without a `filename` URL keyword argument, then the client must set the filename in the `Content-Disposition` HTTP header. For example `Content-Disposition: attachment; filename=upload.jpg`.
+
+**.media_type**: `*/*`
+
+##### Notes:
+
+* The `FileUploadParser` is for usage with native clients that can upload the file as a raw data request. For web-based uploads, or for native clients with multipart upload support, you should use the `MultiPartParser` parser instead.
+* Since this parser's `media_type` matches any content type, `FileUploadParser` should generally be the only parser set on an API view.
+* `FileUploadParser` respects Django's standard `FILE_UPLOAD_HANDLERS` setting, and the `request.upload_handlers` attribute. See the [Django documentation][upload-handlers] for more details.
+
+##### Basic usage example:
+
+ class FileUploadView(views.APIView):
+ parser_classes = (FileUploadParser,)
+
+ def put(self, request, filename, format=None):
+ file_obj = request.FILES['file']
+ # ...
+ # do some staff with uploaded file
+ # ...
+ return Response(status=204)
+
+
---
# Custom parsers
@@ -130,33 +176,19 @@ The following is an example plaintext parser that will populate the `request.DAT
"""
return stream.read()
-## Uploading file content
-
-If your custom parser needs to support file uploads, you may return a `DataAndFiles` object from the `.parse()` method. `DataAndFiles` should be instantiated with two arguments. The first argument will be used to populate the `request.DATA` property, and the second argument will be used to populate the `request.FILES` property.
-
-For example:
+---
- class SimpleFileUploadParser(BaseParser):
- """
- A naive raw file upload parser.
- """
- media_type = '*/*' # Accept anything
+# Third party packages
- def parse(self, stream, media_type=None, parser_context=None):
- content = stream.read()
- name = 'example.dat'
- content_type = 'application/octet-stream'
- size = len(content)
- charset = 'utf-8'
+The following third party packages are also available.
- # Write a temporary file based on the request content
- temp = tempfile.NamedTemporaryFile(delete=False)
- temp.write(content)
- uploaded = UploadedFile(temp, name, content_type, size, charset)
+## MessagePack
- # Return the uploaded file
- data = {}
- files = {name: uploaded}
- return DataAndFiles(data, files)
+[MessagePack][messagepack] is a fast, efficient binary serialization format. [Juan Riaza][juanriaza] maintains the [djangorestframework-msgpack][djangorestframework-msgpack] package which provides MessagePack renderer and parser support for REST framework.
+[jquery-ajax]: http://api.jquery.com/jQuery.ajax/
[cite]: https://groups.google.com/d/topic/django-developers/dxI4qVzrBY4/discussion
+[upload-handlers]: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#upload-handlers
+[messagepack]: https://github.com/juanriaza/django-rest-framework-msgpack
+[juanriaza]: https://github.com/juanriaza
+[djangorestframework-msgpack]: https://github.com/juanriaza/django-rest-framework-msgpack