0

In the early versions of AFNetworking if I had to make my own custom client then I would simply inherit from AFHTTPClient and create my methods. In AFNetworking 2.0 I believe I need to inherit from AFHTTPSessionManager.

@interface MyCustomClient : AFHTTPSessionManager
    {

    }

In my situation I need to send in request as soap. This means that HTTP Body will be soap and HTTP HEADERS will be text/xml.

Let's say I have a variable which contains the entire soap body I need to send to the server.

NSString *soapBody = @"Soap body";

Using my custom class defined above which inherits from AFHTTPSessionManager how will I set the soap body to the Request HTTPBody.

If there is anyway to access NSURLRequest from inside the AFHTTPSessionManager then I can simply do setHTTPBody but it seems there is not?

I hope I am making sense now!

john doe
  • 9,220
  • 23
  • 91
  • 167

2 Answers2

6

You should create a subclass of AFHTTPRequestSerializer, then implement the protocol AFURLRequestSerialization, this class is going to care about adding the body and headers to the request

 - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                                withParameters:(id)parameters
                                         error:(NSError * __autoreleasing *)error
{
     NSParameterAssert(request);

     if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
         return [super requestBySerializingRequest:request withParameters:parameters error:error];
     }

     NSMutableURLRequest *mutableRequest = [request mutableCopy];

     [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
         if (![request valueForHTTPHeaderField:field]) {
             [mutableRequest setValue:value forHTTPHeaderField:field];
         }
     }];


     [mutableRequest setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
     [mutableRequest setHTTPBody:@"This is the soap Body!!"];

     return mutableRequest;
}

You can read the implementation or other AFHTTPRequestSerializer subclasses https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFURLRequestSerialization.m#L1094

JE Herrejon
  • 402
  • 3
  • 10
  • But soap body can be different for different calls. Also is there no way to do this without subclassing. AFNetworking 2.0 is not well designed I guess. – john doe Feb 12 '14 at 01:08
  • 1
    The problem is Objective-C there is no native way to build soap body, you can make that class to build the proper body by reading the WSDL or making templates of the bodies. – JE Herrejon Feb 12 '14 at 01:30
  • 1
    This is the correct approach. @johndoe You're mistaken. AFNetworking is designed to perfectly accommodate this. Additional method parameters can be used to encode any additional information needed to construct a SOAP body. – mattt Feb 12 '14 at 21:02
3

Init a NSMutableURLRequest *request; and set httpBody to request by: [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

You can try this code.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]];
    request.HTTPMethod = @"POST";
    [request setValue:@"application/soap+xml;charset = utf-8" forHTTPHeaderField:@"Content-Type"];
    request.HTTPBody = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
              if (!error) {
                  // parse response here
              } else {
                  // error
              }
          }];
    [sessionTask resume];

hope it help You.

larva
  • 4,687
  • 1
  • 24
  • 44
  • I tried the following: but it didn't work. Did I miss something here, plz have a look : NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://tempuri.org/"]]; [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; AFHTTPSessionManager *sManager = [[AFHTTPSessionManager alloc] init]; – Anoop Vaidya Oct 28 '15 at 12:28
  • ` [sManager dataTaskWithRequest:(NSURLRequest *)request completionHandler:^( NSURLResponse *response, NSData *data, NSError *error){ NSLog(@"%@", data); }];` – Anoop Vaidya Oct 28 '15 at 12:30
  • You can see the proper codes in [this question](http://stackoverflow.com/questions/33389940/afhttpsessionmanager-to-use-soap-based-service) – Anoop Vaidya Oct 28 '15 at 12:33
  • @AnoopVaidya with your question, seem some thing wrong with XML format. what is character 'n' in final at "n" and "n” ? – larva Oct 29 '15 at 01:39