新しい時間エントリを投稿するために、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 引数の受け渡しによって正しく構築されていないことだけだと思いますか?
どんな助けでも大歓迎です!
ありがとう。