Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.data in DRF vs request.body in Django

Django REST framework introduces a Request object that extends the regular HttpRequest, this new object type has request.data to access JSON data for 'POST', 'PUT' and 'PATCH' requests.

However, I can get the same data by accessing request.body parameter which was part of original Django HttpRequest type object.

One difference which I see is that request.data can be accessed only one time. This restriction doesnt apply to request.body.

My question is what are the differences between the two. What is preferred and what is the reason DRF provides an alternative way of doing same thing when There should be one-- and preferably only one --obvious way to do it.

UPDATE: Limiting the usecase where body is always of type JSON. Never XML/ image or conventional form data. What are pros/cons of each?

like image 645
iankit Avatar asked Apr 14 '16 07:04

iankit


People also ask

What is request data in DRF?

Django REST framework introduces a Request object that extends the regular HttpRequest, this new object type has request. data to access JSON data for 'POST', 'PUT' and 'PATCH' requests. However, I can get the same data by accessing request. body parameter which was part of original Django HttpRequest type object.

What is the difference between Django and DRF?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django.

What is request data?

Data request means a discovery procedure in which the requesting party asks another person for specified information or requests the production of documents.

What is the type of request in Django?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.


1 Answers

You should use request.data. It's more flexible, covers more use cases and it can be accessed as many times as needed. Quoting the docs:

Aboout request.data

REST framework introduces a Request object that extends the regular HttpRequest, and provides more flexible request parsing. The core functionality of the Request object is the request.data attribute, which is similar to request.POST, but more useful for working with Web APIs.

request.POST # Only handles form data. Only works for 'POST' method.

request.data # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods.

About request.body

The raw HTTP request body as a byte string. This is useful for processing data in different ways than conventional HTML forms: binary images, XML payload etc. For processing conventional form data, use HttpRequest.POST.

So unless you want to handle binary images or XML payload, never use request.body, it'll only be a simple string containing, well, the body of the request. Always use request.data which'll be the fully parsed body (i.e. a Python dict) which is much more convenient to handle.

like image 145
Seb D. Avatar answered Sep 19 '22 18:09

Seb D.