无法在后端解码POST请求正文:raise MultiValueDictKeyError(key)

2024-09-24 00:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我将POST请求体中的JSON数据发送到Python后端(Django)。在后端,我收到以下内容:

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

{"holdingTime":0,"asma40":49,"asma60":18,"temperature":9,"visibility":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-01-01 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)

错误:

Internal Server Error: /batch_predict Traceback (most recent call last): File "/Users/tuter/anaconda3/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/Users/tuter/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/tuter/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/tuter/anaconda3/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/Users/tuter/Desktop/test/backend/views.py", line 192, in batch_predict_endpoint body = json.loads(body_unicode) File "/Users/tuter/anaconda3/lib/python3.6/json/init.py", line 354, in loads return _default_decoder.decode(s) File "/Users/tuter/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Users/tuter/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 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)

Tags: inpyjsonresponserequestliblineunicode
1条回答
网友
1楼 · 发布于 2024-09-24 00:26:05

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

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

相关问题 更多 >