我为我的服务生成了正确的json输出。我已经在浏览器中复制并粘贴了restkit输出以用于restservice,它工作得很好。但是,我得到了
{"questions": ["This field is required."]}
我的嵌套数组字段,用于尝试从restkit发布时RKResponsDescriptor的响应。如果我带走了问题关系,我的帖子就可以了。如果我的json格式与所有正确的字段一致,我无法理解为什么帖子不起作用。我正在使用django-rest-framework进行休息服务。
如何从Restkit成功发布问题字段?
这是来自Restkit的我的json。
{
"questions" : [
{
"question_text" : "What is the question text?",
"question_description" : "What is the question?",
"driver_id" : 9
}
],
"id" : 9,
"first_name" : "Mark",
"phone" : "6783333333",
"last_name" : "Cuban",
"city" : "Decatur",
"email" : "cuban@gmail.com",
"home_address" : "3144 Topawa Pl.",
"state" : "Florida",
"zipcode" : "30033"
}
这是我的映射:
RKEntityMapping *driverMapping = [RKEntityMapping mappingForEntityForName:@"Driver" inManagedObjectStore:managedObjectStore];
[driverMapping addAttributeMappingsFromDictionary:@{ @"id":@"driverID", @"email":@"email", @"last_name":@"lastName", @"first_name":@"firstName", @"phone":@"phone", @"home_address":@"homeAddress", @"city":@"city", @"state":@"state", @"zipcode":@"zip"}];
driverMapping.identificationAttributes = @[@"driverID"];
RKEntityMapping *questionMapping = [RKEntityMapping mappingForEntityForName:@"Question" inManagedObjectStore:managedObjectStore];
[questionMapping addAttributeMappingsFromDictionary:@{@"driver_id":@"driver.driverID",@"question_description":@"questionDescription", @"question_text":@"questionText"}];
[driverMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"questions" toKeyPath:@"questions" withMapping:questionMapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:driverMapping
pathPattern:@"/drivers/"
keyPath:nil
statusCodes:statusCodes];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:driverMapping objectClass:[Driver class] rootKeyPath:nil];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
这是我的请求发送到服务器。我用查尔斯来检索这个。
city Decatur
email cuban@gmail.com
first_name Mark
home_address 3144 Topawa Pl.
id 9
last_name Cuban
phone 6783333333
questions[][driver_id] 9
questions[][question_description] What is the question?
questions[][question_text] What is the question text?
state Florida
zipcode 30033
以下是Charles的原始数据。
POST /drivers/ HTTP/1.1
Host: localhost:8888
Authorization: Basic andoaW5lczpXZWJzdGVycGllMQ==
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Accept-Encoding: gzip, deflate
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
Accept: application/json
Content-Length: 304
Connection: keep-alive
User-Agent: mydriver/1.0 (iPad Simulator; iOS 6.1; Scale/2.00)
city=Decatur&email=cuban%40gmail.com&first_name=Mark&home_address=3144%20Topawa%20Pl.&id=9&last_name=Cuban&phone=6783333333&questions[][driver_id]=9&questions[][question_description]=What%20is%20the%20question%3F&questions[][question_text]=What%20is%20the%20question%20text%3F&state=Florida&zipcode=30033
答案 0 :(得分:1)
目前尚不清楚您是否实际发送JSON,因为您从Charles粘贴的数据没有特定格式。检查标题和“JSON文本”选项卡(如果存在)。
另外,更改您的请求描述符:
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[driverMapping inverseMapping] objectClass:[Driver class] rootKeyPath:nil];