24

I'd like to write a Django view which serves out variant content based on what's requested. For example, for "text/xml", serve XML, for "text/json", serve JSON, etc. Is there a way to determine this from a request object? Something like this would be awesome:

def process(request):
    if request.type == "text/xml":
        pass
    elif request.type == "text/json":
        pass
    else:
        pass

Is there a property on HttpRequest for this?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

5 Answers5

21

'Content-Type' header indicates media type send in the HTTP request. This is used for requests that have a content (POST, PUT).

'Content-Type' should not be used to indicate preferred response format, 'Accept' header serves this purpose. To access it in Django use: HttpRequest.META.get('HTTP_ACCEPT')

See more detailed description of these headers

Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
Jan Wrobel
  • 6,969
  • 3
  • 37
  • 53
21

HttpRequest.META, more specifically HttpRequest.META.get('HTTP_ACCEPT') — and not HttpRequest.META.get('CONTENT_TYPE') as mentioned earlier

Roshan Mathews
  • 5,788
  • 2
  • 26
  • 36
  • 4
    I know this is old, but although this is the accepted answer, it is not correct. As is mentioned in Jan's comment below, the correct method is the 'Accept' header, accessible via `HttpRequest.META.get('HTTP_ACCEPT')`. – Mr. B Sep 27 '13 at 18:20
  • 2
    You could do a fallback, to handle both cases (plus a default): `request.META.get('HTTP_ACCEPT', request.META.get('CONTENT_TYPE', 'application/your_default'))` – Mr. B Sep 27 '13 at 18:27
  • I don't know if something has changed but I seem to have to write: `request.META.get('headers').get("HTTP_ACCEPT")` – jonalv Feb 27 '19 at 08:54
  • as of 2020 you can use `HttpRequest.content_type` – DataGreed May 20 '20 at 19:03
5

As said in other answers, this information is located in the Accept request header. Available in the request as HttpRequest.META['HTTP_ACCEPT'].

However there is no only one requested content type, and this header often is a list of accepted/preferred content types. This list might be a bit annoying to exploit properly. Here is a function that does the job:

import re

def get_accepted_content_types(request):
    def qualify(x):
        parts = x.split(';', 1)
        if len(parts) == 2:
            match = re.match(r'(^|;)q=(0(\.\d{,3})?|1(\.0{,3})?)(;|$)',
                             parts[1])
            if match:
                return parts[0], float(match.group(2))
        return parts[0], 1

    raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
    qualified_content_types = map(qualify, raw_content_types)
    return (x[0] for x in sorted(qualified_content_types,
                                 key=lambda x: x[1], reverse=True))

For instance, if request.META['HTTP_ACCEPT'] is equal to "text/html;q=0.9,application/xhtml+xml,application/xml;q=0.8,*/*;q=0.7". This will return: ['application/xhtml+xml', 'text/html', 'application/xml', '*/*'] (not actually, since it returns a generator).

Then you can iterate over the resulting list to select the first content type you know how to respond properly.

Note that this function should work for most cases but do not handle cases such as q=0 which means "Not acceptable".

Sources: HTTP Accept header specification and Quality Values specification

Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
3

in django 1.10, you can now use, request.content_type, as mentioned here in their doc

yyforyongyu
  • 95
  • 1
  • 6
1

As of Django 3.1, there is the HttpRequest.accepts(mime_type) method which determines whether a particular content type is acceptable by the client.

There is a useful example linked by that documentation which shows how a CreateView could support JSON responses:

from django.http import JsonResponse
from django.views.generic.edit import CreateView
from myapp.models import Author


class JsonableResponseMixin:
    """
    Mixin to add JSON support to a form.
    Must be used with an object-based FormView (e.g. CreateView)
    """

    def form_invalid(self, form):
        response = super().form_invalid(form)
        if self.request.accepts("text/html"):
            return response
        else:
            return JsonResponse(form.errors, status=400)

    def form_valid(self, form):
        # We make sure to call the parent's form_valid() method because
        # it might do some processing (in the case of CreateView, it will
        # call form.save() for example).
        response = super().form_valid(form)
        if self.request.accepts("text/html"):
            return response
        else:
            data = {
                "pk": self.object.pk,
            }
            return JsonResponse(data)


class AuthorCreateView(JsonableResponseMixin, CreateView):
    model = Author
    fields = ["name"]
ffff
  • 81
  • 5