AFNetworking - Making an XML POST request to REST service? How to form NSDictionary parameters?

百般思念 提交于 2019-12-11 03:52:23

问题


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.


回答1:


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];


来源:https://stackoverflow.com/questions/10437432/afnetworking-making-an-xml-post-request-to-rest-service-how-to-form-nsdiction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!