0

我已经为我的服务生成了正确的 json 输出。我已经在浏览器中复制并粘贴了 restservice 的 restkit 输出,它工作得很好。但是,我得到

{"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

这是查尔斯的原始数据。

   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
4

1 回答 1

1

目前尚不清楚您是否真的发送 JSON,因为您从 Charles 粘贴的数据没有特定格式。检查标题和“JSON 文本”选项卡(如果存在)。

另外,更改您的请求描述符:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[driverMapping inverseMapping] objectClass:[Driver class] rootKeyPath:nil];
于 2013-07-03T06:42:55.563 回答