Search code examples
pythondjangohttpcontent-type

How to handle with Django HTTP.Request, request content-type, query parameters


Hi all I'm new in Python and Django actually also in Coding.

I would like to build an app, that can receive a POST-Request with the Content_Type 'application/xml'.

I don't understand how to handle with the HTTP.Request.META in django. First I would like to check the Content_type, then the Query_string, then Content_Lenght.

from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
from django.http import (
HttpResponse, HttpResponseNotAllowed, HttpRequest,)


@csrf_exempt
# Check the HTTP Request Method
def app(request):
    if request.method != "POST":
        return HttpResponseNotAllowed(permitted_methods=('POST',))
    else:
       # checkcontent(request)
    return HttpResponse('OK')

“““
def checkcontent(request):
    if not 'application/xml' in request.meta['CONTENT_TYPE']:
        raise Exception("Invalid content-type. The expected request content-type is 'application/xml'")

“““

The Comment Block Doesn't Work!

Can someone explain me?

Thx


Solution

  • first of all, here are all available headers of http request in django

    so you need:

    request.META['CONTENT_TYPE']
    

    instead of

    request.meta['CONTENT_TYPE']