我有一个请求网址http://my_ip:8080,我有自定义标题和正文
正文为{"test":"hello"}
,我无法读取POST请求的正文。和我的观点模块为,
my views.py
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.utils.decorators import method_decorator
from django.db import connection
import json
import re
from hinduja.models import Register
# Register API for Hinduja
@method_decorator(csrf_exempt)
def hinduja_register(req):
agent = is_valid_agent(req)
if agent["result"] == True:
try:
body = get_body(req)
except Exception as e:
print("Exception as " + str(e))
json_data = {"result":"test"}
else:
json_data = error_message('invalid_client')
return response_to_client(json_data)
def is_valid_agent(req):
regex_http_ = re.compile(r'^HTTP_.+$')
regex_content_type = re.compile(r'^CONTENT_TYPE$')
regex_content_length = re.compile(r'^CONTENT_LENGTH$')
request_header = {}
agent = {}
for header in req.META:
if regex_http_.match(header) or regex_content_type.match(header) or regex_content_length.match(header):
request_header[header] = req.META[header]
try:
user_agent = request_header['HTTP_USER_AGENT']
if user_agent == 'XXX':
agent["result"] = True
agent["user_agent"] = user_agent
except Exception as e:
agent["result"] = False
return agent
# Get the request body
def get_body(req):
body_unicode = req.body.decode('utf-8')
body = json.loads(body_unicode)
return body;
# Return error response
def error_message(message):
return {"result":"error", "message": message}
# Return to success response to client
def response_to_client(json_data):
data = json.dumps(json_data)
return HttpResponse(data, content_type='application/json')
打电话
body_unicode = req.body.decode('utf-8')
我收到超时错误并且
来自('10 .1.1.120',47496)的管道无法解决此错误。任何人都可以给我关于这个问题的建议
答案 0 :(得分:0)
是否应该这样:
def get_body(req):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
return body;