# Automatic request parser decorator for JSON/XML format

**URL:** <https://forum.djangoproject.com/t/automatic-request-parser-decorator-for-json-xml-format/1015>\
**Category:** Django Internals\
**Created:** [January 30, 2020, 5:22pm UTC](https://forum.djangoproject.com/t/automatic-request-parser-decorator-for-json-xml-format/1015 "2020-01-30T17:22:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Genarito](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/genarito/32/800_2.png) [@Genarito](https://forum.djangoproject.com/u/Genarito)\
**Post date:** [January 30, 2020, 5:22pm UTC](https://forum.djangoproject.com/t/automatic-request-parser-decorator-for-json-xml-format/1015/1 "2020-01-30T17:22:33Z")

</div>

Hi, I’ve worked with Django for several months and I haven’t see a quick way to parse a request’s body which can be in JSON o XML format. Until now I’ve had to do:

```auto
import json

def some_view(request):
    if request.content_type == 'application/json':
        data = json.loads(request.body)
        # Uses 'data' variable as we can't set parsed data to request.body
    else:
        # Uses request.POST[...], or request.GET[...], etc

```

In cases where It’s expected to have multiple formats (an application which uses JQuery will send the request in `application/x-www-form-urlencoded` format, modern apps will send in json, and so on) it would be very cool to have a Django’s decorator to parse automatically especified formats and HTTP methods and set it to the request’s body to work with `request.POST/GET/etc` as a `QueryDict` instance.

The usage would be something like this:

```auto
from django.http import parse_request

@parse_request(['json', 'xml'], ['POST', 'GET', 'PATCH'])
def some_view(request):
    field = request.POST['field']

```

What do you think about it? Would you like me to make a PR with this new decorator?

Thanks in advance and sorry about my English

---

<div class="post-metadata">

**Author:** ![conor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/conor/32/3736_2.png) [@conor](https://forum.djangoproject.com/u/conor)\
**Post date:** [January 30, 2020, 7:14pm UTC](https://forum.djangoproject.com/t/automatic-request-parser-decorator-for-json-xml-format/1015/2 "2020-01-30T19:14:14Z")

</div>

Hi Genarito,

Your English is nothing to apologise for!

I’ve never worked on Django’s internals, but I do use Django Rest Framework a lot for working with JSON. It’s a sensation when it comes to JSON and serialising your models.

[https://www.django-rest-framework.org/](https://www.django-rest-framework.org/)

I believe it also can work well with XML

[https://www.django-rest-framework.org/api-guide/renderers/#xml](https://www.django-rest-framework.org/api-guide/renderers/#xml)

And let me apologise if you were already aware of DRF. Just thought I’d mention it in case you hadn’t had the pleasure to work with it.

Cheers,

C

---

<div class="post-metadata">

**Author:** ![Genarito](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/genarito/32/800_2.png) [@Genarito](https://forum.djangoproject.com/u/Genarito)\
**Post date:** [January 31, 2020, 1:52pm UTC](https://forum.djangoproject.com/t/automatic-request-parser-decorator-for-json-xml-format/1015/3 "2020-01-31T13:52:20Z")

</div>

Hi Conor! First of all, thank you for answering! Secondly, I’ve worked with DRF and It’s an excelent tool. I didn’t know the existence of [JSONP](https://www.django-rest-framework.org/api-guide/renderers/#jsonp), I’ll give a try!

Anyway I think that It would be nice to have a simpler solution, a Django’s decorator would be easier to use, avoiding import a new dependency to the project or set a middleware.
