AFJSONRequestOperation not working (postpath works)

339 views Asked by At

I am trying to post a JSON using AFNetworking.

Here's the code that im using:

+ (RESTAPI *)sharedClient
{
    static RESTAPI *_sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"https://mybaseurl.com"]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self setParameterEncoding:AFJSONParameterEncoding];
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];
    [self setAllowsInvalidSSLCertificate:YES];

    return self;
}

The following code does not works. Everytime i try i get the following error:

The operation couldn’t be completed. (NSURLErrorDomain error -1012.)

// this code does not works
// 
- (void)loginNOTWORKING
{
    RESTAPI *client = [RESTAPI sharedClient];

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

    NSDictionary *parameter = @{@"tgout": @"1",
                                @"tgin": @2,
                                @"username": @"foo",
                                @"password":@"bar"};
    NSURLRequest *request = [client requestWithMethod:@"POST" path:@"/login" parameters:parameter];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        // code for successful return goes here
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
        NSLog(@"THIS IS NEVER CALLED: %@", JSON);
        // do something with return data
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        // code for failed request goes here
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
        NSLog(@"SAD, VERY SAD: %@", error.localizedDescription);
        // do something on failure
    }];

    [operation start];
}

This code works:

// this code WORKS
- (void)loginWORKING
{
    RESTAPI *client = [RESTAPI sharedClient];

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

    NSDictionary *parameter = @{@"tgout": @"1",
                                @"tgin": @2,
                                @"username": @"foo",
                                @"password":@"bar"};

    [client postPath:@"/login" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text
        NSLog(@"IT WORKS: %@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Response: %@", error.localizedDescription);
    }];
}

Why the first login method does not works? What am i doing wrong?

2

There are 2 answers

3
zbMax On

Try by replacing

NSURLRequest *request = [client requestWithMethod:@"POST" path:@"/login" parameters:parameter];

with

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/*HERE THE URL STRING TO CALL*/"]]
0
CouchDeveloper On

You can find the error -1012 in file CFNetworkErrors.h:

kCFURLErrorUserCancelledAuthentication = -1012
"The connection failed because the user cancelled required authentication."

I guess, there is an issue with your authentication. The error description is possibly misleading with regard to "the user" - it is actually a delegate method that gets invoked which cancels the authentication, or the authentication simply fails.

This of course can be caused by not properly serializing the parameters. I would suggest to use a lower level API, create the request manually, encode the JSON manually with NSJSONSerialization, and set the body data and the URL of the request. IMHO, this is certainly more readable code, and likely requires less code.