问题: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的项目,所以任何额外的信息都将非常受欢迎。
谢谢,
答案 0 :(得分:0)
我在这里做错了两件事。
在我的代码中,我创建了作为字典发送的有效负载,我使用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)
{[formdata appendPartWithHeaders:nil body:jSenderData];} 成功:^(NSURLSessionDataTask * task,id responseObject)
我要做的第二件事是使用多部分表单请求。我试图将JSON数据作为多部分表单的一部分发送。这未在服务器上正确解释。
感谢社区的帮助。