Django 不接受来自客户端的 JSON 数据
Django not accepting JSON data from the client
问题:recJSON = json.loads(request.body) 抛出异常。
def addPatient(request):
if request.method == 'POST':
# Convert JSON to python objects and
# store into the DB
print request.encoding
#body_unicode = request.body.decode('utf-8').encode('utf-16')
#try:
recJSON = json.loads(request.body)
#except ValueError:
# print "%s",ValueError
print "%s",request.body
return HttpResponse(json.dumps(request.body),content_type="application/json")
异常:
recJSON = json.loads(request.body)
File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 505, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "/usr/local/lib/python2.7/dist-packages/simplejson/scanner.py", line 127, in scan_once
return _scan_once(string, idx)
File "/usr/local/lib/python2.7/dist-packages/simplejson/scanner.py", line 118, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
来自客户:
使用 AFNetworking 并使用以下方式发送请求 POST:
// Create JSON data
NSMutableDictionary* sendID = [[NSMutableDictionary alloc] init ];
[sendID setObject:senderID forKey:@"SenderID"];
NSMutableDictionary* sendDispName = [[NSMutableDictionary alloc] init ];
[sendDispName setObject:displayName forKey:@"SenderDispName"];
NSMutableDictionary* sendMessage = [[NSMutableDictionary alloc] init ];
[sendMessage setObject:text forKey:@"Message"];
NSMutableDictionary* sendDate = [[NSMutableDictionary alloc] init ];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[sendDate setObject:[dateFormatter stringFromDate:date] forKey:@"SendDate"];
NSMutableArray* dataToSend = [[NSMutableArray alloc] init];
[dataToSend addObject:sendID];
[dataToSend addObject:sendDispName];
[dataToSend addObject:sendMessage];
[dataToSend addObject:sendDate];
NSData* jSenderData = [NSJSONSerialization dataWithJSONObject:dataToSend options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"%@", [[NSString alloc] initWithData:jSenderData encoding:NSUTF8StringEncoding]);
[_manager.requestSerializer setValue:@"TOKEN" forHTTPHeaderField:@"X-CSRFTOKEN"];
[_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[_manager POST:@"addPatient/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formdata)
{ [formdata appendPartWithHeaders:nil body:jSenderData];}
success:^(NSURLSessionDataTask *task, id responseObject)
我读到 UTF-8 编码与 json.dumps(request.body) 有问题,已在 Python 3.0 中修复
http://bugs.python.org/issue11489
我面临的是同样的问题吗?这是我使用 Django 和 Python 的第一个项目,因此非常感谢任何额外信息。
谢谢,
这里有两件事我做错了。
在我的代码中,我正在创建要作为字典发送的有效负载,并且我正在使用 AFJSONRequestSerializer 和后来的 NSJSONSerialization 来获取 NSData(传递给请求结构)。所以换句话说,我试图创建 JSON 的 JSON 数据。
NSData* jSenderData = [NSJSONSerialization dataWithJSONObject:dataToSend options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"%@", [[NSString alloc] initWithData:jSenderData encoding:NSUTF8StringEncoding]);
[_manager.requestSerializer setValue:@"TOKEN" forHTTPHeaderField:@"X-CSRFTOKEN"];
[_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[_manager POST:@"addPatient/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formdata)
{ [表单数据 appendPartWithHeaders:nil body:jSenderData];}
成功:^(NSURLSessionDataTask *task, id responseObject)
我要做的第二件事是使用多部分表单请求。我试图将 JSON 数据作为多部分形式的主体发送。这在服务器上没有得到正确的解释。
感谢社区的帮助。
问题:recJSON = json.loads(request.body) 抛出异常。
def addPatient(request):
if request.method == 'POST':
# Convert JSON to python objects and
# store into the DB
print request.encoding
#body_unicode = request.body.decode('utf-8').encode('utf-16')
#try:
recJSON = json.loads(request.body)
#except ValueError:
# print "%s",ValueError
print "%s",request.body
return HttpResponse(json.dumps(request.body),content_type="application/json")
异常:
recJSON = json.loads(request.body)
File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 505, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/local/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "/usr/local/lib/python2.7/dist-packages/simplejson/scanner.py", line 127, in scan_once
return _scan_once(string, idx)
File "/usr/local/lib/python2.7/dist-packages/simplejson/scanner.py", line 118, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
来自客户:
使用 AFNetworking 并使用以下方式发送请求 POST:
// Create JSON data
NSMutableDictionary* sendID = [[NSMutableDictionary alloc] init ];
[sendID setObject:senderID forKey:@"SenderID"];
NSMutableDictionary* sendDispName = [[NSMutableDictionary alloc] init ];
[sendDispName setObject:displayName forKey:@"SenderDispName"];
NSMutableDictionary* sendMessage = [[NSMutableDictionary alloc] init ];
[sendMessage setObject:text forKey:@"Message"];
NSMutableDictionary* sendDate = [[NSMutableDictionary alloc] init ];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[sendDate setObject:[dateFormatter stringFromDate:date] forKey:@"SendDate"];
NSMutableArray* dataToSend = [[NSMutableArray alloc] init];
[dataToSend addObject:sendID];
[dataToSend addObject:sendDispName];
[dataToSend addObject:sendMessage];
[dataToSend addObject:sendDate];
NSData* jSenderData = [NSJSONSerialization dataWithJSONObject:dataToSend options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"%@", [[NSString alloc] initWithData:jSenderData encoding:NSUTF8StringEncoding]);
[_manager.requestSerializer setValue:@"TOKEN" forHTTPHeaderField:@"X-CSRFTOKEN"];
[_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[_manager POST:@"addPatient/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formdata)
{ [formdata appendPartWithHeaders:nil body:jSenderData];}
success:^(NSURLSessionDataTask *task, id responseObject)
我读到 UTF-8 编码与 json.dumps(request.body) 有问题,已在 Python 3.0 中修复 http://bugs.python.org/issue11489
我面临的是同样的问题吗?这是我使用 Django 和 Python 的第一个项目,因此非常感谢任何额外信息。
谢谢,
这里有两件事我做错了。
在我的代码中,我正在创建要作为字典发送的有效负载,并且我正在使用 AFJSONRequestSerializer 和后来的 NSJSONSerialization 来获取 NSData(传递给请求结构)。所以换句话说,我试图创建 JSON 的 JSON 数据。
NSData* jSenderData = [NSJSONSerialization dataWithJSONObject:dataToSend options:NSJSONWritingPrettyPrinted error:&error]; NSLog(@"%@", [[NSString alloc] initWithData:jSenderData encoding:NSUTF8StringEncoding]); [_manager.requestSerializer setValue:@"TOKEN" forHTTPHeaderField:@"X-CSRFTOKEN"]; [_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [_manager POST:@"addPatient/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formdata)
{ [表单数据 appendPartWithHeaders:nil body:jSenderData];} 成功:^(NSURLSessionDataTask *task, id responseObject)
我要做的第二件事是使用多部分表单请求。我试图将 JSON 数据作为多部分形式的主体发送。这在服务器上没有得到正确的解释。
感谢社区的帮助。