iosからjsonをdjangoのローカルサーバーにpostしたいのですがrequest.POSTするとkeyerrorになり取得できません。
ios側でどうすればkeyを付加できるのでしょうか?
swift
1コード 2 let url = "http://192.168.10.13:8000/pic/" 3 var request = NSMutableURLRequest(url: NSURL(string: url)! as URL) 4 5 // set the method(HTTP-POST) 6 request.httpMethod = "POST" 7 // set the header(s) 8 request.addValue("application/json", forHTTPHeaderField: "Content-Type") 9 10 // set the request-body(JSON) 11 var params = Dictionary<String,Any>() 12 params["x"] = originalTapPoint.x 13 params["y"] = originalTapPoint.y 14 do{ 15 request.httpBody = try JSONSerialization.data(withJSONObject: params, options: []) 16 }catch{ 17 print(error.localizedDescription) 18 } 19 20 // use NSURLSessionDataTask 21 let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {data, response, error in 22 if (error == nil) { 23 let result = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)! 24 print(result) 25 } else { 26 print(error) 27 } 28 }) 29 task.resume() 30 31
error log
[19/Jul/2017 03:41:04] "GET /media/pic/pic_for_calib.JPG HTTP/1.1" 304 0 b'{"y":152.5,"x":183.5}' Internal Server Error: /pic/ Traceback (most recent call last): File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/utils/datastructures.py", line 83, in __getitem__ list_ = super(MultiValueDict, self).__getitem__(key) KeyError: 'result' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/hoge.virtualenvs/neochi/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper return bound_func(*args, **kwargs) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2) File "/Users/hoge/neochi/neochi_web/calibrator/views.py", line 17, in dispatch return super(PicView, self).dispatch(request, *args, **kwargs) File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch return handler(request, *args, **kwargs) File "/Users/hoge/neochi/neochi_web/calibrator/views.py", line 42, in post jsondata = request.POST['result'] File "/Users/hoge/.virtualenvs/neochi/lib/python3.6/site-packages/django/utils/datastructures.py", line 85, in __getitem__ raise MultiValueDictKeyError(repr(key)) django.utils.datastructures.MultiValueDictKeyError: "'result'"