在python中向HTTP POST请求添加和访问标头

时间:2017-11-14 08:33:45

标签: python django rest django-rest-framework

我和django 1.11.7有休息框架。我一直试图访问POST请求标题,但是当我打印它时,我得到了“无”#39;我正在使用申请邮递员发送请求。我在Headers部分添加了一个键值对,请求代码如下所示:

POST /create_user HTTP/1.1
Host: localhost:8000
Content-Type: application/json
key1: value1
Cache-Control: no-cache
Postman-Token: # Postman token

{
  "username": "abcdefgh@123"
}

在我的django应用程序中,我的views.py文件如下所示:

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
import json

# Create your views here.
class CreateUser(APIView):
    def post(self, request, format='json'):
        try:
            request_data = json.loads(request.body.decode(encoding='UTF-8'))
            print 'Header data:', request.POST.get('key1')

&#39>请求'变量的类型为Request(Django Rest Framework对象)。没有例外。那么如何访问POST' key1'头?

0 个答案:

没有答案