RestKit POST 对象到服务器,但服务器收到空的 GET 请求
Posted
技术标签:
【中文标题】RestKit POST 对象到服务器,但服务器收到空的 GET 请求【英文标题】:RestKit POSTs object to server, but server receives empty GET request 【发布时间】:2013-01-12 20:51:54 【问题描述】:当我在 RestKit 中使用 postObject
方法时,日志显示一切正常(见下文)。
但在服务器上,请求以GET
接收,请求正文为空,而不是POST
并包含序列化对象。
2013-01-12 14:40:08.860 AppName[3953:c07] I restkit.network:RKHTTPRequestOperation.m:152 POST 'http://urlgoeshere.com'
下面是我用于POST
和RestKit 初始化的代码。我正在使用 RestKit 0.20.0-pre6。
POST 对象到服务器
// CREATE THE MANAGED OBJECT, AND SAVE IT
RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] managedObjectStore];
Message *msg = [NSEntityDescription insertNewObjectForEntityForName:@"Message" inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
msg.note = @"This is a sample message.";
msg.dateCreated = [NSDate date];
[objectStore.mainQueueManagedObjectContext save:nil];
// BUILD THE REQUEST MAPPING & REQUEST DESCRIPTOR
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@
@"messageId": @"id",
@"dateCreated": @"date_created",
];
[requestMapping addAttributeMappingsFromArray:@[ @"note" ]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Message class] rootKeyPath:@"message"];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
// SEND POST REQUEST TO SERVER
[[RKObjectManager sharedManager] postObject:msg
path:@"/path/to/messages"
parameters:[NSDictionary dictionaryWithObject:[(AppDelegate*)[[UIApplication sharedApplication] delegate] userId] forKey:@"user_id"]
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"success!");
failure:^(RKObjectRequestOperation *operation, NSError *error)
NSLog(@"failure.\n\n%@", [error description]);
];
App Delegate 中的 RestKit 初始化
NSURL *baseURL = [NSURL URLWithString:@"http://urlgoeshere.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// Setup our object mappings
RKEntityMapping *messageMapping = [RKEntityMapping mappingForEntityForName:@"Message" inManagedObjectStore:managedObjectStore];
messageMapping.identificationAttributes = @[ @"messageId" ];
[messageMapping addAttributeMappingsFromDictionary:@
@"id": @"messageId",
@"date_created": @"dateCreated",
];
[messageMapping addAttributeMappingsFromArray:@[ @"note" ]];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"DBNameHere.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
【问题讨论】:
【参考方案1】:您在与哪种服务器通信?我怀疑您的服务器正在执行重定向或其他操作。 RestKit 使用 NSURLConnection 发出请求,因此问题极不可能出在客户端,因为您在日志中看到的报告是直接从 NSURLRequest 读取的。
【讨论】:
感谢您的快速回复,布莱克。你是绝对正确的。当我访问“/path”时,它在服务器上重定向到“/path/”。这就是为什么GET
请求在 RestKit 中可以正常工作,但其他任何请求方法都不能正常工作。感谢您的帮助!以上是关于RestKit POST 对象到服务器,但服务器收到空的 GET 请求的主要内容,如果未能解决你的问题,请参考以下文章
RestKit POST 发送 JSON 不包含映射路径/类名
Post Json Restkit 无法为 HTTP 方法“POST”类型的对象找到可路由路径
RestKit 0.20.0-rc1 - POST 后重复对象
RestKit 和 fetchedResultsController performFetch 成功但 fetchedObjects 为空