【问题标题】:How to handle with Django HTTP.Request, request content-type, query parameters如何处理 Django HTTP.Request、请求内容类型、查询参数
【发布时间】:2016-08-17 06:35:57
【问题描述】:

大家好,我是 Python 和 Django 的新手,实际上也是 Coding。

我想构建一个应用程序,它可以接收带有 Content_Type 'application/xml' 的 POST-Request。

我不明白如何在 django 中处理 HTTP.Request.META。 首先我想检查 Content_type,然后是 Query_string,然后是 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'")

“““

评论区不起作用!

谁能解释一下?

谢谢

【问题讨论】:

    标签: python django http content-type


    【解决方案1】:

    首先,here are all available headersdjango中的http请求

    所以你需要:

    request.META['CONTENT_TYPE']
    

    而不是

    request.meta['CONTENT_TYPE']
    

    【讨论】:

      猜你喜欢
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多