2

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.

Community
  • 1
  • 1
skålfyfan
  • 4,931
  • 5
  • 41
  • 59

1 Answers1

0

While my answer is not timely, I hope it helps others that encounter this issue. Below is an example of how to make an XML POST request using 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];
Alex
  • 599
  • 1
  • 4
  • 16