简体   繁体   中英

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

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM