rest_framework.request

The Request class is used as a wrapper around the standard request object.

The wrapped request then offers a richer API, in particular :

  • content automatically parsed according to Content-Type header, and available as request.data
  • full support of PUT method, including support for file uploads
  • form overloading of HTTP method, content type and content

Module Contents

is_form_media_type(media_type)

Return True if the media type is a valid form media type.

class override_method(view, request, method)

Bases:object

A context manager that temporarily overrides the method on a request, additionally setting the view.request attribute.

Usage:

with override_method(view, request, ‘POST’) as request:
… # Do stuff with view and request
__enter__(self)
__exit__(self, *args, **kwarg)
exception WrappedAttributeError

Bases:Exception

wrap_attributeerrors()

Used to re-raise AttributeErrors caught during authentication, preventing these errors from otherwise being handled by the attribute access protocol.

class Empty

Bases:object

Placeholder for unset attributes. Cannot use None, as that may be a valid value.

_hasattr(obj, name)
clone_request(request, method)

Internal helper method to clone a request, replacing with a different HTTP method. Used for checking permissions against other methods.

class ForcedAuthentication(force_user, force_token)

Bases:object

This authentication class is used if the test client or request factory forcibly authenticated the request.

authenticate(self, request)
class Request(request, parsers=None, authenticators=None, negotiator=None, parser_context=None)

Bases:object

Wrapper allowing to enhance a standard HttpRequest instance.

Kwargs:
  • request(HttpRequest). The original request instance.
  • parsers_classes(list/tuple). The parsers to use for parsing the request content.
  • authentication_classes(list/tuple). The authentications used to try authenticating the request’s user.
content_type
stream

Returns an object that may be used to stream the request content.

query_params

More semantically correct name for request.GET.

data
user

Returns the user associated with the current request, as authenticated by the authentication classes provided to the request.

auth

Returns any non-user authentication information associated with the request, such as an authentication token.

successful_authenticator

Return the instance of the authentication instance class that was used to authenticate the request, or None.

DATA
POST
FILES
QUERY_PARAMS
_default_negotiator(self)
_load_data_and_files(self)

Parses the request content into self.data.

_load_stream(self)

Return the content body of the request, as a stream.

_supports_form_parsing(self)

Return True if this requests supports parsing form data.

_parse(self)

Parse the request content, returning a two-tuple of (data, files)

May raise an UnsupportedMediaType, or ParseError exception.

_authenticate(self)

Attempt to authenticate the request using each authentication instance in turn.

_not_authenticated(self)

Set authenticator, user & authtoken representing an unauthenticated request.

Defaults are None, AnonymousUser & None.

__getattr__(self, attr)

If an attribute does not exist on this instance, then we also attempt to proxy it to the underlying HttpRequest object.

force_plaintext_errors(self, value)