Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#36245 closed New feature (duplicate)

Implement Configurable Content Type Parsing `request.data` to Modernize the HTTPRequest

Reported by: Adya Owned by: Adya
Component: HTTP handling Version: dev
Severity: Normal Keywords: parsing, http
Cc: Adya Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adya)

Adding a new feature to modernize the HTTPRequest object, adding a content-type aware request.data property. This will parse request.body according to the content type.

In the initial phase, I will add request.data and add support for JSON body handling, but the next phase is to make that fully pluggable with custom parsers.

This Feature Idea is described in the Django GSoC 2025 Wiki page https://code.djangoproject.com/wiki/SummerOfCode2025#ConfigurableContentTypeParsing

This kind of feature is already implemented in *Django REST Framework* I have read the source code, DRF, Django docs, MDN reference on HTTP, and implemented it in my local Django project.
A [PlanEnglish.io Python blog](https://python.plainenglish.io/django-rest-api-tutorial-mastering-parsers-with-practical-examples-d24872c47849) described it so well.

Outcome after completing this feature
Developers will have a more streamlined and efficient way to access request data
For instance, with request.body, a developer working with JSON data would need to write

import json
data = json.loads(request.body)
user_name = data.get('name')

With the proposed request.data, this simplifies to something like:

user_name = request.data.get('name')

I am creating the test cases and patches for this implementation, then will PR it

Implementation Plan:

  • Write test cases
  • Add the request.data property to HTTPRequest.
  • Implement initial JSON parsing logic.

_I AM ASKING FOR TRIAGE_

Change History (11)

comment:1 by Adya, 6 months ago

Description: modified (diff)

comment:2 by Adya, 6 months ago

Description: modified (diff)

comment:3 by Adya, 6 months ago

Description: modified (diff)

comment:4 by Adya, 6 months ago

Description: modified (diff)

comment:5 by Sarah Boyce, 6 months ago

Resolution: duplicate
Status: assignedclosed

Thank you! I think this is a duplicate of #21442

in reply to:  5 comment:6 by Adya, 6 months ago

Replying to Sarah Boyce:

Thank you very much for the comment, I had already checked this ticket, maybe there was a confusion .

I think PR and discussion in ticket https://code.djangoproject.com/ticket/21442, It is not active after some progress for the last 5 months. And according to Django GSoC Idea page it is mentioned that contributors have to accomplish it in two phases. So I opened a ticket for the initial phase. It is mentioned here https://code.djangoproject.com/wiki/SummerOfCode2025#ConfigurableContentTypeParsing

Please note that there is no any open ticket mention on page, that's why I reported one

But this idea is proposed in Django GSoC 2025 and I am working on it, on the current Django Development version.

*I am discussing in the Forum too https://forum.djangoproject.com/u/adyaprasad/summary*

For the Initial phase I have implemented changes in django/http/request.py and django/http/response.py
In django/http/request.py something like (reference short code)

class HttpRequest:
    # ... existing code ...
    
    @property
    def data(self):
        """
        Parses and returns the request body based on Content-Type.
        
        Currently supports:
        * 'application/json': Returns the parsed JSON data
        * All other content types: Returns an empty dictionary
        """
        if not hasattr(self, '_cached_data'):
            content_type = self.headers.get('Content-Type', '')
            # Extract the main content type without parameters
            content_type = content_type.split(';')[0].strip()
            
            if content_type == 'application/json':
                if not self.body:
                    self._cached_data = {}
                else:
                    try:
                        self._cached_data = json.loads(self.body.decode('utf-8'))
                    except (ValueError, UnicodeDecodeError) as exc:
                        from django.core.exceptions import BadRequest
                        raise BadRequest(f"JSON parse error - {str(exc)}")
            else:
                # For the initial phase, return empty dict for non-JSON content
                self._cached_data = {}
                
        return self._cached_data

I can change the code if need and not fit
My proposal and research paper are almost ready, I am working on the Test Cases

Please help me, what should I do now? Either assigned me ticket #21442 and opened and assigned me this ticket
What would you like to recommend?
🤞

Last edited 6 months ago by Adya (previous) (diff)

comment:7 by Adya, 6 months ago

Resolution: duplicate
Status: closednew

comment:8 by Natalia Bidart, 6 months ago

Resolution: duplicate
Status: newclosed
Version: 5.1dev

Hello Adya,

This Django ticket tracker is for confirmed bugs and accepted feature work, not for GSoC project tracking or mentorship. Ticket #21442 is the official ticket for this feature, and it is already assigned and has some work in progress. Any work on it should be coordinated through the Django Forum or the GSoC platform, not by opening new tickets in Trac.

If you need guidance on your proposal, please continue discussions in the Django Forum and eventually submit your proposal through GSoC's official platform. Trac is not the place for proposal reviews, test case discussions, or mentorship.

Thank you!

comment:9 by Adya, 6 months ago

Hey Natalia😊, Thanks for this confirmation.
I get confused because this ticket is not mentioned in the content Idea description. https://code.djangoproject.com/wiki/SummerOfCode2025#ConfigurableContentTypeParsing.

But in the description a statement is given:

The initial phase — targeted for before GSoC — will add request.data and add support for JSON body handling, but the next phase is to make that fully pluggable with custom parsers.

What about the initial stage requirements, should I leave it? Will this problem occur if I did not complete an initial phase statement myself? Or it would be okay to leave OR #21442 ticket will get counted as initial phase completion.

Just tell me what I should do now?

in reply to:  9 ; comment:10 by Natalia Bidart, 6 months ago

Replying to Adya:

Hey Natalia😊, Thanks for this confirmation.
I get confused because this ticket is not mentioned in the content Idea description. https://code.djangoproject.com/wiki/SummerOfCode2025#ConfigurableContentTypeParsing.

Yes, sorry about that, there was a misunderstanding and despite this topic was listed in the wiki page, it's not really available for GSoC. Some extra data:

So at this point there is no much to do on this topic except to potentially contribute to the DEP to get that finalized and approved.

in reply to:  10 comment:11 by Adya, 6 months ago

Replying to Natalia Bidart:

Yes, sorry about that, there was a misunderstanding and despite this topic was listed in the wiki page, it's not really available for GSoC. Some extra data:

So at this point there is no much to do on this topic except to potentially contribute to the DEP to get that finalized and approved.

Thanks again for this direction, Natalia. There are 5 ideas listed the wiki page currently, could you recommend any idea or ticket to work (ticket: triage ticket and unassigned).
Main concern is that the idea or ticket on which I could get a potential mentor. In the forum, mentor unavailability is a major concern. So I need to choose a idea for which I could get a mentor

Note: See TracTickets for help on using tickets.
Back to Top