【问题标题】:Reskit POST operation not working proper (error 1016)Reskit POST 操作无法正常工作(错误 1016)
【发布时间】:2013-06-26 20:45:39
【问题描述】:

我正在尝试将 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];

已创建一行并正确填充了字段。但我仍然收到内容类型错误

【问题讨论】:

  • 服务器期待什么?此外,服务器应该使用 JSON 响应,但它实际上发送文本(可能是错误消息,但未显示)。打开跟踪日志记录并使用日志记录添加故障块。
  • 我使用跟踪日志编辑了我的帖子。我还添加了带有日志记录的故障块,但错误与我的帖子中的相同
  • 目的是获取服务器发回的错误文本,它应该告诉你哪里出了问题。直到您知道自己不知道要解决什么问题为止。
  • 实际上似乎服务器端一切都很好:[29/Jun/2013 07:08:48] "POST /api/v1/user/ HTTP/1.1" 201 0
  • 那么为什么它没有响应 JSON 呢?

标签: ios restkit content-type tastypie


【解决方案1】:

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

【讨论】:

  • 好吧,我的错误,我误解了你的最后一个 cmets ...正如你指出的那样,我的服务器没有以 JSON 响应,而是以 HTML 响应。我需要在 Tastypie 上添加 always_return_data = True 并相应地修改我的 RKResponseDescriptor。感谢您的帮助@Wain。
  • 我如何告诉restkit不要期望接收JSON?我无法控制 api 返回标头.. 我还注意到更改 RKObjectManager requestSerializationMIMEType 没有任何区别.. 我看错地方了吗?
  • 我什至尝试过 setAcceptHeaderWithMIMEType@"text/html" 但没有运气.. 在我的其他项目中这很奇怪,当我将它与 AFNetworking 一起使用时(即没有 restkit 的东西)
猜你喜欢
  • 1970-01-01
  • 2021-10-26
  • 2019-01-11
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
相关资源
最近更新 更多