Sebastian L
Sebastian L

Reputation: 924

Issue with post request with form-data using AFNetworking to https server

I have ran into an issue that I can't solve myself. I am trying to make a POST request with form-data using AFNetworking to our backend using https but I get some error that I don't understand. I have been logging the http traffic and it doesn't seem to even send a request, only a CONNECT request which I think has something to do with the certificate.

This is my code:

NSDictionary *params = @{@"client_id"     : @"2",
                             @"grant_type"    : @"password",
                             @"email" : username,
                             @"password" : password};

    AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    [policy setValidatesDomainName:NO];
    [policy setAllowInvalidCertificates:YES];
    [policy setValidatesDomainName:NO];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = policy;
    [manager POST:ssoURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

This is the error:

Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7fc3b582c4c0 {NSErrorFailingURLKey=https://myaccount-dev.<domain>/oauth/token, NSErrorFailingURLStringKey=https://myaccount-dev.<domain>/oauth/token}

How do I fix this?

Upvotes: 0

Views: 262

Answers (1)

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19802

You can allow invalid certificates here:

AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:@"URL"];
client.allowsInvalidSSLCertificate = YES;

For AFHTTPRequestOperationManager

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;

Upvotes: 2

Related Questions