2013-06-29 32 views
1

我试图用Django-Tastypie创建的REST API绑定RestKit。 GET操作正常,但POST操作有奇怪的行为:在数据库上创建一行,但所有字段都为空。Reskit POST操作无法正常工作(错误1016)

我已经在版本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 = @"[email protected]"; 
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] 

有人已经面临这种问题?

Thx寻求帮助。

编辑:

这里的很多记录与跟踪记录实现:

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":"[email protected]"}} 

编辑2:

通过编辑这一行:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"objects"]; 

by

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:nil]; 

行被创建并且字段被正确填充。但我仍然得到内容类型错误

+0

服务器期待什么?此外,服务器应该使用JSON响应,但它实际发送的文本(可能是错误消息,但未显示)。打开跟踪记录并添加记录失败块。 – Wain

+0

我使用跟踪日志编辑了我的帖子。我还在日志中添加了失败块,但是错误与我在文章 – Yaman

+0

中相同。目的是让服务器返回错误文本,告诉你哪里出了问题。直到你知道你不知道要修复什么。 – Wain

回答

1

错误只是告诉RestKit希望接收JSON,但服务器不发送JSON(或至少不设置正确的标头)。解决此问题的最佳方法是更改​​服务器响应中的标头,使其返回JSON。或者,告诉RestKit不要期望收到JSON。

+0

好吧,我的坏,我误解了你最后的评论...正如你指出的,我的服务器没有在JSON响应,但在HTML。我需要在Tastypie上添加'always_return_data = True'并相应地修改我的RKResponseDescriptor。 Thx为你的帮助@Wain。 – Yaman

+1

以及我如何告诉restkit不要期望收到JSON?我没有控制的API返回标题..我也注意到,更改'RKObjectManager' requestSerializationMIMEType'没有任何区别..我看着错误的地方? – abbood

+1

我甚至试过[setAcceptHeaderWithMIMEType](https://github.com/RestKit/RestKit/blob/development/Code/Network/RKObjectManager.h?source=c)到'@“text/html”'但没有运气..这是奇怪的B/C在我的其他项目,当我用AFNetworking这样工作(即没有restkit的东西) – abbood