我将POST请求正文中的JSON数据发送到Python后端(Django)。在后端,我收到以下内容:
body_unicode = request.body.decode('utf-8')
print(body_unicode)
{“ holdingTime”:0,“ asma40”:49,“ asma60”:18,“温度”:9,“可见性”:9999,“ windIntensity”:1.5,“ windDirection”:“ VRB”,“ airportDepDelay “:8,” csvData“:” NUM,AIRLINE_ARR_ICAO,WAKE,SIBT,SOBT,PLANNED_TURNAROUND,DISTANCE_FROM_ORIGIN,DISTANCE_TO_TARGET \ n1,AEA,H,2016年1月1日 04:05:00,2016-01-01 14:10:00,605,9920.67,5776.89 \ n2,AEA,H,2016-01-01 04:25:00,2016-01-01 06:30:00,125.0,10060.80,483.93 \ n3,AVA,H,2016-01-01 05:05:00,2016-01-01 07:05:00,120.0,8033.86,8033.86 \ n4,IBE,H,2016-01-01 05:20:00,2016-01-01 10:40:00,320.0,6000.00,8507.73 \ n5,IBE,H,2016-01-01 05:25:00,2016-01-01 10:50:00,325.0,6698.42,6698.42 \ n6,IBE,H,2016-01-01 05:30:00,2016-01-01 08:10:00,160.0,10699.06,1246.30 \ n7,IBE,H,2016-01-01 05:30:00,2016-01-01 11:00:00,330.0,9081.35,8033.86 \ n8,IBE,H,2016-01-01 05:40:00,2016-01-01 11:35:00,355.0,5776.89,8749.87 \ n9,ANE,M,2016-01-01 05:50:00,2016-01-01 14:50:00,540.0,284.73,284.73 \ n10,ETD,H,2016-01-01 06:35:00,2016-01-01 08:00:00,85.0,5647.10,5647.10 \ n11,IBS,M,2016-01-01 06:50:00,2016-01-01 08:00:00,70.0,547.36,1460.92 \ n12,IBE,H,2016-01-01 06:50:00,2016-01-01 10:35:00,225.0,6763.16,6763.16 \ n13,IBE,H,2016-01-01 06:50:00,2016-01-01 10:50:00,240.0,7120.40,7120.40 \ n14,IBE,H,2016-01-01 06:50:00,2016-01-01 10:55:00,245.0,7010.08,6000.00 \ n15,QTR,H,2016-01-01 06:55:00,2016-01-01 08:30:00,95.0,5338.52,5338.52 \ n16,IBS,M,2016-01-01 07:00:00,2016-01-01 07:45:00,45.0,485.52,1721.09 \ n17,IBS,M,2016-01-01 07:00:00,2016-01-01 07:45:00,45.0,394.98,429.37 \ n18,ELY,M,2016-01-01 07:05:00,2016-01-01 08:30:00,85.0,3550.48,3550.48 \ n19,AAL,H,2016-01-01 07:05:00,2016-01-01 12:05:00,300.0,5925.61,5925.61 \ n20,TVF,M,2016-01-01 07:30:00,2016-01-01 08:10:00,40.0,1030.31,1030.31 \ n“}
但是,如果执行此操作,则会出现错误:
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
print(body)
错误:
内部服务器错误:/ batch_predict追溯(最近一次调用) 最后):文件 “ /Users/tuter/anaconda3/lib/python3.6/site-packages/django/core/handlers/exception.py”, 第34行,在内部 response = get_response(request)文件“ /Users/tuter/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py”, _get_response中的第126行 响应= self.process_exception_by_middleware(e,request)文件“ /Users/tuter/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py”, _get_response中的第124行 响应= wraped_callback(request,* callback_args,** callback_kwargs)文件“ /Users/tuter/anaconda3/lib/python3.6/site-packages/django/views/decorators/csrf.py”, 第54行,在wrapped_view中 返回view_func(* args,** kwargs)文件“ /Users/tuter/Desktop/test/backend/views.py”,第192行,在 batch_predict_endpoint body = json.loads(body_unicode)文件“ /Users/tuter/anaconda3/lib/python3.6/json/init.py”,第354行,在 负载 返回_default_decoder.decode文件“ /Users/tuter/anaconda3/lib/python3.6/json/decoder.py”,行339,在 解码 obj,end = self.raw_decode(s,idx = _w(s,0).end())文件“ /Users/tuter/anaconda3/lib/python3.6/json/decoder.py”,第357行,在 raw_decode 从None引发JSONDecodeError(“期望值”,s,err.value)json.decoder.JSONDecodeError:期望值:第1行第1列(字符 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 :(得分:0)
在django中,您不需要使用json包加载请求正文,您可以通过json键从请求中获取数据:
def my_view(request):
sample_data = request.POST['sample_key']
# statements