简体   繁体   中英

AFNetworking 2.x POST request with xml body

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM