代码之家  ›  专栏  ›  技术社区  ›  ScalaBoy

无法在后端解码post请求正文

  •  0
  • ScalaBoy  · 技术社区  · 6 年前

    我将post请求体中的json数据发送到python后端(django)。在后端,我接收到以下内容:

    body_unicode = request.body.decode('utf-8')
    print(body_unicode)
    

    {“保持时间”:0,“asma40”:49,“asma60”:18,“温度”:9,“能见度”:9999,“风强度”:1.5,“风向”:“VRB”,“机场延迟”:8,“CSVData”:“num,航空公司Arr_u Icao,Wake,Sibt,Sobt,计划的_u周转,距离_u Origin,距离_u Target\n1,AEA,H,2016-01-01 04:05:002016-01-01 14:10:006059920.675776.89\N2,AEA,H,2016-01-01 04:25:002016-01-01 06:30:00125.010060.80483.93\n3,ava,h,2016-01-01 05:05:002016-01-01 07:05:00120.08033.868033.86\n4,IBE,H,2016-01-01 05:20:002016-01-01 10:40:00320.06000.008507.73\n5,IBE,H,2016-01-01 05:25:002016-01-01 10:50:00325.06698.426698.42\n6,IBE,H,2016-01-01 05:30:002016-01-01 08:10:00160.010699.061246.30\n7,IBE,H,2016-01-01 05:30:002016-01-01 11:00:00330.09081.358033.86\n8,IBE,H,2016-01-01 05:40:002016-01-01 11:35:00355.05776.898749.87\n9,ane,m,2016-01-01 05:50:002016-01-01 14:50:00540.0284.73284.73\n10,等时,小时,2016-01-01 06:35:002016-01-01 08:00:00,85.05647.105647.10\n11,IBS,M,2016-01-01 06:50:002016-01-01 08:00:00,70.0547.361460.92\n12,IBE,H,2016-01-01 06:50:00 2016-01-01 10:35:00225.06763.166763.16\n13,IBE,H,2016-01-01 06:50:00 2016-01-01 10:50:00240.07120.407120.40\n14,IBE,H,2016-01-01 06:50:00 2016-01-01 10:55:00245.07010.086000.00\n15,季度,小时,2016-01-01 06:55:002016-01-01 08:30:00,95.05338.525338.52\n16,IBS,M,2016-01-01 07:00:002016-01-01 07:45:00,45.0485.521721.09\n17,IBS,M,2016-01-01 07:00:002016-01-01 07:45:00,45.0394.98429.37\n18,伊利,M,2016-01-01 07:05:002016-01-01 08:30:00,85.03550.483550.48\n19,AAL,H,2016-01-01 07:05:00 2016-01-01 12:05:00300.05925.615925.61\n20,tvf,m,2016-01-01 07:30:002016-01-01 08:10:00,40.01030.311030.31\n“}

    但如果我这样做,我会犯错误:

    body_unicode = request.body.decode('utf-8')
    body = json.loads(body_unicode)
    print(body)
    

    错误:

    内部服务器错误:/批处理预测回溯(最近的调用 最后):文件 “/users/tuter/anaconda3/lib/python3.6/site packages/django/core/handlers/exception.py”, 34号线,内部 response=get_response(request)file“/users/tuter/anaconda3/lib/python3.6/site packages/django/core/handlers/base.py”, 第126行,in_get_response response=self.process_exception_by_中间件(e,请求)文件“/users/tuter/anaconda3/lib/python3.6/site packages/django/core/handlers/base.py”, 124号线,收到回复 response=wrapped_callback(请求,*callback_args,**callback_kwargs)文件“/users/tuter/anaconda3/lib/python3.6/site packages/django/views/decorators/csrf.py”, 54行,包裹视图 return view_func(*args,**kwargs)file“/users/tuter/desktop/test/backend/views.py”,第192行,in 批处理预测终结点 body=json.loads(body_unicode)文件“/users/tuter/anaconda3/lib/python3.6/json/ 初始化 .py“,354行,英寸 荷载 返回默认解码文件“/users/tuter/anaconda3/lib/python3.6/json/decoder.py”,第339行,in 译码 obj,end=self.raw_decode(s,idx=w(s,0.end())文件“/users/tuter/anaconda3/lib/python3.6/json/decoder.py”,第357行,in 罗氏译码 从none json.decoder.jsondecodeerror引发jsondecodeerror(“期望值”,s,err.value):期望值:第1行第1列(char 0)

    更新:

    如果我这样做:

    sample_data = request.POST['csvData']
    
    print("sample_data",sample_data)
    

    然后出现此错误:

        sample_data = request.POST['csvData']
      File "/Users/tuter/anaconda3/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
        raise MultiValueDictKeyError(key)
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Khashayar Ghamati    6 年前

    在django中,不需要用json包加载请求体,您可以通过json键从请求中获取数据:

    def my_view(request):
       sample_data = request.POST['sample_key']
       # statements