-2

How can i perform following operation in AFNetworking "2.5". I wrote this code in older AFNetworking version:

-(void)login{

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            username.text,@"username",password.text,@"password",nil];

    AFHTTPClient *httpClient= [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"some_url"]]];

    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
    [httpClient setAuthorizationHeaderWithToken:@"Token"];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"some_path" parameters:params];

    [request addValue:@"some_value" forHTTPHeaderField:@"some_field"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"Response- %@", responseObject);
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error- %@",error);
     }];
    [operation start];
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Bista
  • 7,869
  • 3
  • 27
  • 55

1 Answers1

3

This is what i did in AFNetworking 2.5.

-(void)login
{    
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                        username.text,@"email",password.text,@"password",nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:some_value forHTTPHeaderField:@"some_field"];

    [manager POST:@"some_url" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
    {
        NSLog(@"JSON: %@", responseObject);
    } 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    {
        NSLog(@"Error: %@", error);
   }];
}
Bista
  • 7,869
  • 3
  • 27
  • 55