简体   繁体   English

如何处理 Django HTTP.Request、请求内容类型、查询参数

[英]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.大家好,我是 Python 新手,Django 实际上也在编码领域。

I would like to build an app, that can receive a POST-Request with the Content_Type 'application/xml'.我想构建一个应用程序,它可以接收带有 Content_Type 'application/xml' 的 POST 请求。

I don't understand how to handle with the HTTP.Request.META in django.我不明白如何处理 Django 中的 HTTP.Request.META。 First I would like to check the Content_type, then the Query_string, then Content_Lenght.首先,我想检查 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'")

“““

The Comment Block Doesn't Work!评论区不起作用!

Can someone explain me?有人可以解释我吗?

Thx谢谢

first of all, here are all available headers of http request in django首先, 这里是django 中所有可用的http请求

so you need:所以你需要:

request.META['CONTENT_TYPE']

instead of而不是

request.meta['CONTENT_TYPE']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用django.test.Client发出没有Content-Type标头的HTTP请求? - How can I make an HTTP request with no Content-Type header using django.test.Client? 如何通过http.request的json参数双引号将dictonary对象传递给Django-rest-framework? - How to pass the dictonary object to Django-rest-framework with double quote by http.request's json parameter? 如何在 FastAPI 请求 header 中限制内容类型 - How to restrict content-type in FastAPI request header 强制 Content-Type 或公开 Flask 中已知内容类型的 request.data - Force Content-Type or expose request.data in Flask for known content-type urllib.Request 删除 Content-Type 标头 - urllib.Request remove Content-Type header HEAD 请求在重定向时缺少内容类型 (301) - HEAD request missing content-type on redirect (301) 烧瓶中没有POST请求和Content-Type“application / json”的响应 - No response with POST request and Content-Type “application/json” in flask 发送请求时出现 Content-Type 错误 - I get a Content-Type error while sending a request 使用Multipart / form-data的POST请求。内容类型不正确 - POST request with Multipart/form-data. Content-type not correct Content-Type 请求 header 错误处理未正确返回 - Content-Type request header error handling not returning correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM