我正在尝试创建对 REST API 的 POST 请求以发布新的时间条目。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
我认为我的问题只是没有通过 NSDictionary 参数传递正确构建帖子的 XML 吗?
任何帮助将不胜感激!
谢谢。