2013-06-29 34 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