I am trying to create a POST request to a REST API to post a new time entry. The required XML structure for the post request is as follows:
<time-entry>
<person-id>#{person-id}</person-id>
<date>#{date}</date>
<hours>#{hours}</hours>
<description>#{description}</description>
</time-entry>
With AFNetworking is the XML encoding done behind the scenes just from passing an NSDictionary of parameters to the POST request? Here is my code so far:
From my 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]);
}];
}
Yes. My APIClient operation(s) are configured for XML via:
[self registerHTTPOperationClass:[AFXMLRequestOperation class]];
Right now every time I try to POST with this code I always get a 422 response code failure which I believe means the entity passed was unprocessable. https://stackoverflow.com/a/3291292/312411
I think my problem is just that the XML for the post isn't being built correctly through the NSDictionary params argument passing ?
Any help would GREATLY be appreciated!
Thanks.