Search code examples
iosobjective-cjsonafnetworkingafnetworking-3

How can I migrate in AFNetworking 3.0?


I am migrating in AFNetworking 3.0. I am using AFHTTPRequestOperation in AFNetworking but it was removed in recent updates. I tried all the possible solution. Basically I need to post a JSON with Header. I convert my NSDIctionary into a JSON object then add it as string. Here is the sample JSON with Header of mine

RequestHeader={
  "lname" : "sadhsdf",
  "bday" : "2003-03-13",
  "age" : "12",
  "address" : "dsfhskdjgds",
  "gender" : "M",
  "cnumber" : "12312412",
  "fname" : "sadhsdf",
  "RequestType" : "userregistration",
  "Email" : "sldjlkasd@sjdhflksdf.com",
  "status" : "dsfhskdjgds",
  "Password" : "123456"
}

RequestHeader is an NSString and the rest are NSDictionary.

How can I apply it in AFNetworking 3.0? Thank you in advance.


Solution

  • NSURL *url = [NSURL URLWithString:@"https://example.com/"];
    
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
    
                            height, @"user[height]",
    
                            weight, @"user[weight]",
    
                            nil];
    
    [httpClient postPath:@"/myobject" parameters:params 
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
        NSLog(@"Request Successful, response '%@'", responseStr);
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
    }];