2016-11-23 4 views
1

私はリクエストのURLがhttp://my_ip:8080で、私のカスタムヘッダーと本文は 本文になっており、{"test":"hello"}となっており、POSTリクエストの本文を読み取ることができません。そして、としての私の見解モジュール、Djangoで本文を読む

私の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') 

私はタイムアウトエラーを持って、('10から

壊れたパイプを持っ

body_unicode = req.body.decode('utf-8')

呼び出し中.1.1.120 '、47496)このエラーは解決できません。誰も私にこの問題

+0

DjangoはHTTPメッセージの完全なボディを受信して​​いません。このエラーが一貫して表示される場合は、クライアントまたはサーバー構成に問題がある可能性があります。 – knbk

+0

私は自分のsettings.pyでALLOWED_HOSTSを設定しました –

+0

'req.body'を印刷して、どのパラメータが来るのか調べてみましょう。 –

答えて

0

が、それはこのようにあるべき提案についてを与えることができます:

def get_body(req): 
    body_unicode = request.body.decode('utf-8') 
    body = json.loads(body_unicode) 
    return body; 
+0

私は同じコードを使用しました –

関連する問題