gpt4 book ai didi

ios - Reskit POST 操作无法正常工作(错误 1016)

转载 作者:行者123 更新时间:2023-11-29 13:11:02 26 4
gpt4 key购买 nike

我正在尝试将 RestKit 与 Django-Tastypie 创建的 REST API 绑定(bind)。GET 操作运行良好,但 POST 操作有奇怪的行为:在数据库中创建了一行,但所有字段都是空的。

我的 RestKit 版本为 0.20.3-dev

这是我的代码(直接从 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];

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

最佳答案

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

关于ios - Reskit POST 操作无法正常工作(错误 1016),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com