2

新しい時間エントリを投稿するために、REST API への POST 要求を作成しようとしています。投稿リクエストに必要な XML 構造は次のとおりです。

<time-entry>
  <person-id>#{person-id}</person-id>
  <date>#{date}</date>
  <hours>#{hours}</hours>
  <description>#{description}</description>
</time-entry>

AFNetworking では、パラメーターの NSDictionary を POST 要求に渡すだけで、XML エンコーディングが舞台裏で行われますか? これまでの私のコードは次のとおりです。

私の APIClient から:

-(void)postTimeEntry:(TLActiveWork *)work {

[self setAuthorizationHeaderWithUsername:[self.activeUser username] 
   password:[self.activeUser password]];

// Most of the following is static data. Just trying to get a successful POST at the 
// moment.
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSString stringWithFormat:@"%@",[[self activeUser] personId]], @"person-id",
                        [NSString stringWithFormat:@"%@",[NSDate date]], @"date",
                        @"1", @"hours",
                        @"testing...!", @"description",
                        nil];

NSDictionary *wrapper = [NSDictionary dictionaryWithObjectsAndKeys:
                         params, @"time-entry",
                         nil];

 NSLog(@"Params: %@", wrapper);

// AFNetworking call!

[self postPath:[NSString stringWithFormat:@"/projects/%@/time_entries.xml",
   [[work project] projectId]] parameters:wrapper 
   success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"-----SUCCESS!!!-----");

} failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failure: %ld - %@", [operation.response statusCode], 
           [error description]);
}];
}

はい。私の APIClient 操作は、次の方法で XML 用に構成されています。

[self registerHTTPOperationClass:[AFXMLRequestOperation class]];

現在、このコードで POST を試みるたびに、常に 422 応答コード エラーが発生します。これは、渡されたエンティティが処理不能であったことを意味すると考えています。https://stackoverflow.com/a/3291292/312411

私の問題は、投稿の XML が NSDictionary params 引数の受け渡しによって正しく構築されていないことだけだと思いますか?

どんな助けでも大歓迎です!

ありがとう。

4

1 に答える 1

0

私の答えはタイムリーではありませんが、この問題に遭遇した他の人に役立つことを願っています. XML POST以下は、を使用してリクエストを作成する方法の例ですAFNetworking

AFHTTPClient *HTTPClient = [[AFHTTPClient alloc] initWithBaseURL:@"https://stackoverflow.com"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:HTTPClient.baseURL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSFileManager defaultManager] contentsAtPath:[[NSBundle mainBundle] pathForResource:@"TheNameOfTheXMLFileToSend" ofType:@"xml"]];

AFHTTPRequestOperation *operation = [HTTPClient HTTPRequestOperationWithRequest:request 
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
     // The server responded without an error.
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{
    // The server responded with an error.
}];

[HTTPClient enqueueHTTPRequestOperation:operation];
于 2013-06-28T08:10:02.840 に答える