我正在尝试将RestKit与Django-Tastypie创建的REST API绑定。
GET
操作运行良好,但POST
操作有奇怪的行为:在数据库上创建了一行,但所有字段都是空的。
我在版本0.20.3-dev
上有RestKit
这是我的代码(直接从here复制):
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
[responseMapping addAttributeMappingsFromArray:@[@"pseudo", @"email", @"password"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *userDescriptor2 = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping pathPattern:@"/user" keyPath:@"objects" statusCodes:statusCodes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromArray:@[@"pseudo", @"email", @"password"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"objects"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://127.0.0.1:8000"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:userDescriptor2];
// Tastypie error without this line
manager.requestSerializationMIMEType = RKMIMETypeJSON;
// This line changes nothing
[manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
User *user = [User new];
user.pseudo = @"test";
user.email = @"test@gmail.com";
user.password = @"test";
[manager postObject:user path:@"/api/v1/user/" parameters:nil success:nil failure:nil];
这是我得到的错误:
2013-06-29 02:18:21.882 test_restkit[40395:c07] I restkit:RKLog.m:34 RestKit logging initialized... 2013-06-29 02:18:21.930 test_restkit[40395:c07] I restkit.network:RKObjectRequestOperation.m:180 GET 'http://127.0.0.1:8000/api/v1/post/' 2013-06-29 02:18:21.930 test_restkit[40395:c07] I restkit.network:RKObjectRequestOperation.m:180 POST 'http://127.0.0.1:8000/api/v1/user/' 2013-06-29 02:18:21.965 test_restkit[40395:4c03] E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
"application/x-www-form-urlencoded",
"application/json" )}, got text/html" UserInfo=0xa8437b0 {AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://127.0.0.1:8000/api/v1/user/>, NSErrorFailingURLKey=http://127.0.0.1:8000/api/v1/user/, NSLocalizedDescription=Expected content type {(
"application/x-www-form-urlencoded",
"application/json" )}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x95866b0>} 2013-06-29 02:18:21.966 test_restkit[40395:4c03] E restkit.network:RKObjectRequestOperation.m:243 POST 'http://127.0.0.1:8000/api/v1/user/' (201 Created / 0 objects) [request=0.0304s mapping=0.0000s total=0.0402s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
"application/x-www-form-urlencoded",
"application/json" )}, got text/html" UserInfo=0xa8437b0 {AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://127.0.0.1:8000/api/v1/user/>, NSErrorFailingURLKey=http://127.0.0.1:8000/api/v1/user/, NSLocalizedDescription=Expected content type {(
"application/x-www-form-urlencoded",
"application/json" )}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x95866b0>} 2013-06-29 02:18:22.023 test_restkit[40395:1703] I restkit.network:RKObjectRequestOperation.m:250 GET 'http://127.0.0.1:8000/api/v1/post/' (200 OK / 4 objects) [request=0.0816s mapping=0.0109s total=0.1023s]
有人已经遇到过这种问题吗?
感谢您的帮助。
编辑:
以下是跟踪日志记录启用的日志:
2013-06-29 13:03:28.554 test_restkit[42627:c07] D restkit.object_mapping:RKMappingOperation.m:1021 Finished mapping operation successfully...
2013-06-29 13:03:28.562 test_restkit[42627:c07] T restkit.network:RKObjectRequestOperation.m:178 POST 'http://127.0.0.1:8000/api/v1/user/':
request.headers={
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "test_restkit/1.0 (iPad Simulator; iOS 6.1; Scale/1.00)";
}
request.body={"objects":{"password":"test","pseudo":"test","email":"test@gmail.com"}}
编辑2:
通过编辑此行:
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"objects"];
通过
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:nil];
创建一行并正确填充字段。但我仍然得到内容类型错误
答案 0 :(得分:1)
错误只是你告诉RestKit期望接收JSON,但服务器没有发送JSON(或者至少没有设置正确的标头)。解决此问题的最佳方法是更改服务器响应上的标头,使其返回JSON。或者,告诉RestKit不要期望收到JSON。