AFNetworking 2.x POST request with xml body

2019-07-30 03:41发布

问题:

I am trying to POST some data to the server with body formatted as XML.

[[HttpClientXML sharedClientWithBaseUrl:self.baseUrl] POST:@"/postXml/" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    BSLog(@"Success");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    BSLog(@"Error: %@",error.localizedDescription);
}];

POST body xml content looks smth. like that:

<action name='job'>jobName</action>

HttpClientXML has this definitions:

self.responseSerializer = [AFXMLParserResponseSerializer serializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];

In version 1.x this was doable with AFXMLRequestOperation. But in version 2.x I can not get the working solution with AFHTTPSessionManager.

Any suggestions?

回答1:

Here is the solution...

request.HTTPBody = [self.body dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = @"POST";
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];