Debian Codegraph @

docs/api-guide/content-negotiation.md

raw
1 ---
2 source:
3 - negotiation.py
4 ---
5
6 > HTTP has provisions for several mechanisms for "content negotiation" - the process of selecting the best representation for a given response when there are multiple representations available.
7 >
8 > — [RFC 2616][cite], Fielding et al.
9
10 [cite]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html
11
12 Content negotiation is the process of selecting one of multiple possible representations to return to a client, based on client or server preferences.
13
14 ## Determining the accepted renderer
15
16 REST framework uses a simple style of content negotiation to determine which media type should be returned to a client, based on the available renderers, the priorities of each of those renderers, and the client's `Accept:` header. The style used is partly client-driven, and partly server-driven.
17
18 1. More specific media types are given preference to less specific media types.
19 2. If multiple media types have the same specificity, then preference is given to based on the ordering of the renderers configured for the given view.
20
21 For example, given the following `Accept` header:
22
23 application/json; indent=4, application/json, application/yaml, text/html, */*
24
25 The priorities for each of the given media types would be:
26
27 * `application/json; indent=4`
28 * `application/json`, `application/yaml` and `text/html`
29 * `*/*`
30
31 If the requested view was only configured with renderers for `YAML` and `HTML`, then REST framework would select whichever renderer was listed first in the `renderer_classes` list or `DEFAULT_RENDERER_CLASSES` setting.
32
33 For more information on the `HTTP Accept` header, see [RFC 2616][accept-header]
34
35
36 !!! note
37 "q" values are not taken into account by REST framework when determining preference. The use of "q" values negatively impacts caching, and in the author's opinion they are an unnecessary and overcomplicated approach to content negotiation.
38
39 This is a valid approach as the HTTP spec deliberately underspecifies how a server should weight server-based preferences against client-based preferences.
40
41 ## Custom content negotiation
42
43 It's unlikely that you'll want to provide a custom content negotiation scheme for REST framework, but you can do so if needed. To implement a custom content negotiation scheme override `BaseContentNegotiation`.
44
45 REST framework's content negotiation classes handle selection of both the appropriate parser for the request, and the appropriate renderer for the response, so you should implement both the `.select_parser(request, parsers)` and `.select_renderer(request, renderers, format_suffix)` methods.
46
47 The `select_parser()` method should return one of the parser instances from the list of available parsers, or `None` if none of the parsers can handle the incoming request.
48
49 The `select_renderer()` method should return a two-tuple of (renderer instance, media type), or raise a `NotAcceptable` exception.
50
51 ### Example
52
53 The following is a custom content negotiation class which ignores the client
54 request when selecting the appropriate parser or renderer.
55
56 from rest_framework.negotiation import BaseContentNegotiation
57
58 class IgnoreClientContentNegotiation(BaseContentNegotiation):
59 def select_parser(self, request, parsers):
60 """
61 Select the first parser in the `.parser_classes` list.
62 """
63 return parsers[0]
64
65 def select_renderer(self, request, renderers, format_suffix):
66 """
67 Select the first renderer in the `.renderer_classes` list.
68 """
69 return (renderers[0], renderers[0].media_type)
70
71 ### Setting the content negotiation
72
73 The default content negotiation class may be set globally, using the `DEFAULT_CONTENT_NEGOTIATION_CLASS` setting. For example, the following settings would use our example `IgnoreClientContentNegotiation` class.
74
75 REST_FRAMEWORK = {
76 'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'myapp.negotiation.IgnoreClientContentNegotiation',
77 }
78
79 You can also set the content negotiation used for an individual view, or viewset, using the `APIView` class-based views.
80
81 from myapp.negotiation import IgnoreClientContentNegotiation
82 from rest_framework.response import Response
83 from rest_framework.views import APIView
84
85 class NoNegotiationView(APIView):
86 """
87 An example view that does not perform content negotiation.
88 """
89 content_negotiation_class = IgnoreClientContentNegotiation
90
91 def get(self, request, format=None):
92 return Response({
93 'accepted media type': request.accepted_renderer.media_type
94 })
95
96 [accept-header]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html