Debian Codegraph @

docs/api-guide/format-suffixes.md

raw
1 ---
2 source:
3 - urlpatterns.py
4 ---
5
6 # Format suffixes
7
8 > Section 6.2.1 does not say that content negotiation should be
9 used all the time.
10 >
11 > — Roy Fielding, [REST discuss mailing list][cite]
12
13 A common pattern for Web APIs is to use filename extensions on URLs to provide an endpoint for a given media type. For example, 'http://example.com/api/users.json' to serve a JSON representation.
14
15 Adding format-suffix patterns to each individual entry in the URLconf for your API is error-prone and non-DRY, so REST framework provides a shortcut to adding these patterns to your URLConf.
16
17 ## format_suffix_patterns
18
19 **Signature**: format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None)
20
21 Returns a URL pattern list which includes format suffix patterns appended to each of the URL patterns provided.
22
23 Arguments:
24
25 * **urlpatterns**: Required. A URL pattern list.
26 * **suffix_required**: Optional. A boolean indicating if suffixes in the URLs should be optional or mandatory. Defaults to `False`, meaning that suffixes are optional by default.
27 * **allowed**: Optional. A list or tuple of valid format suffixes. If not provided, a wildcard format suffix pattern will be used.
28
29 Example:
30
31 from rest_framework.urlpatterns import format_suffix_patterns
32 from blog import views
33
34 urlpatterns = [
35 path('', views.apt_root),
36 path('comments/', views.comment_list),
37 path('comments/<int:pk>/', views.comment_detail)
38 ]
39
40 urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
41
42 When using `format_suffix_patterns`, you must make sure to add the `'format'` keyword argument to the corresponding views. For example:
43
44 @api_view(['GET', 'POST'])
45 def comment_list(request, format=None):
46 # do stuff...
47
48 Or with class-based views:
49
50 class CommentList(APIView):
51 def get(self, request, format=None):
52 # do stuff...
53
54 def post(self, request, format=None):
55 # do stuff...
56
57 The name of the kwarg used may be modified by using the `FORMAT_SUFFIX_KWARG` setting.
58
59 Also note that `format_suffix_patterns` does not support descending into `include` URL patterns.
60
61 ### Using with `i18n_patterns`
62
63 If using the `i18n_patterns` function provided by Django, as well as `format_suffix_patterns` you should make sure that the `i18n_patterns` function is applied as the final, or outermost function. For example:
64
65 urlpatterns = [
66 …
67 ]
68
69 urlpatterns = i18n_patterns(
70 format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
71 )
72
73 ---
74
75 ## Query parameter formats
76
77 An alternative to the format suffixes is to include the requested format in a query parameter. REST framework provides this option by default, and it is used in the browsable API to switch between differing available representations.
78
79 To select a representation using its short format, use the `format` query parameter. For example: `http://example.com/organizations/?format=csv`.
80
81 The name of this query parameter can be modified using the `URL_FORMAT_OVERRIDE` setting. Set the value to `None` to disable this behavior.
82
83 ---
84
85 ## Accept headers vs. format suffixes
86
87 There seems to be a view among some of the Web community that filename extensions are not a RESTful pattern, and that `HTTP Accept` headers should always be used instead.
88
89 It is actually a misconception. For example, take the following quote from Roy Fielding discussing the relative merits of query parameter media-type indicators vs. file extension media-type indicators:
90
91 &ldquo;That's why I always prefer extensions. Neither choice has anything to do with REST.&rdquo; &mdash; Roy Fielding, [REST discuss mailing list][cite2]
92
93 The quote does not mention Accept headers, but it does make it clear that format suffixes should be considered an acceptable pattern.
94
95 [cite]: http://tech.groups.yahoo.com/group/rest-discuss/message/5857
96 [cite2]: https://groups.yahoo.com/neo/groups/rest-discuss/conversations/topics/14844