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:
}
}
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.
You have something wrong with your JSON parameters:
kCFURLErrorCannotParseResponse = -1017
This error occurs when you perform a GET
request with the body set (setHTTPBody
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With