我創建了一個應該從curl請求接收發布數據並返回結果的函數。我禁用了視圖的csrf,所以我得到的錯誤是我無法解析json。Django從curl接收發布結果
這是我的看法
@csrf_exempt
def create_user(request):
response = {'status': None}
if request.method == 'POST':
data = json.loads(request.body)
...
我得到的終端
Internal Server Error: /api-user-create/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/samuel/Documents/code/binabikers/delivery/views.py", line 14, in create_user
data = json.loads(request.body)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
[01/Aug/2017 03:58:24] "POST /api-user-create/ HTTP/1.1" 500 85347
這意味着這不是讀捲曲請求JSON這樣
curl -X POST -d "username=john&password=john2&[email protected]&first_name=john&last_name=smith" http://localhost:8000/api-user-create/