Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be

I just started ios development and I'm trying to exchange data with my api. When I'm doing POST requests everything is going fine but when I'm trying to do a GET request I get the following error:

Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be completed. (NSURLErrorDomain error -1017.)" UserInfo=0x145a2c00 {NSErrorFailingURLStringKey=http://myAPI.com/, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://myAPI.com, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x145b21d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1017.)"}

Could someone explain what's going wrong and how I can fix it?

My request:

-(void)hitApiWithURL:(NSString*)url HTTPMethod:(NSString*)HTTPMethod params:(NSDictionary*)params successBlock:(successTypeBlock)success  failureBlock:(errorTypeBlock)failure{


    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];


    [sessionConfig setHTTPAdditionalHeaders:@{@"Content-type": @"application/json"}];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:HTTPMethod];

    // The body
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
    [request setHTTPBody:jsonData];


    NSURLSessionDataTask *dataTaks = [session dataTaskWithRequest:request];
    [dataTaks resume];

    NSLog(@"dataTask started");

}


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
    if (error) {
        //Gives my error
    }
    else {
        // do something:
    }
}
like image 661
Jab Avatar asked Dec 13 '14 12:12

Jab


4 Answers

If you forget to mention HTTPMethod, the error can also take place, I faced the Problem "NSURLErrorDomain Code=-1017", somehow i forget to add line "request.HTTPMethod = "POST".

After adding the line, my code worked perfectly.

like image 164
Nirmala Maurya Avatar answered Nov 14 '22 19:11

Nirmala Maurya


You have something wrong with your JSON parameters:

kCFURLErrorCannotParseResponse = -1017

like image 5
Vitalii Gozhenko Avatar answered Nov 14 '22 18:11

Vitalii Gozhenko


This error occurs when you perform a GET request with the body set (setHTTPBody)

like image 4
AlBeebe Avatar answered Nov 14 '22 18:11

AlBeebe


It isn't direct solution to this case but might help someone with -1017 error.

I had this issue after upgrading Alamofire from 4.8.0 to 5.2.2. Problem was with HTTPHeader.

var authorizationHeader: HTTPHeader {
    guard let session = user?.session else {
        return HTTPHeader(name: "", value: "") 
    }
    return HTTPHeader(name: "Authorization", value: "Bearer \(session)")
}

Apparently sending HTTPHeader with empty key-value i-e HTTPHeader(name: "", value: "") triggers this error. I adjusted my code to instead return nil and it solved the issue.

like image 1
Haseeb Iqbal Avatar answered Nov 14 '22 17:11

Haseeb Iqbal