Reputation: 929
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:
}
}
Upvotes: 5
Views: 19542
Reputation: 4803
There's also a case when you use a VPN to login to your app.
When you're using a VPN, your internet traffic is routed through the VPN's servers. If you lose internet connection or if the VPN server isn't reachable (which was my use-case), the VPN might send back a response that your app isn't equipped to handle, thus resulting in the NSURLErrorCannotParseResponse (-1017) error.
In some cases, VPNs can return custom error messages or codes which your app isn't expecting and therefore can't parse correctly.
I made sure that error.code == NSURLErrorCannotParseResponse
is in fact true which is 1017.
The only thing left is to check with your favorite reachable code if network is reachable:
NetworkReachabilityManager(host: "https://www.google.com")?.isReachable
If:
It means that there is no internet connection
We can also use NEVPNManager to make sure we are in fact using our expected VPN.
Upvotes: 0
Reputation: 654
For me when send one empty header like ["":""]. Triggers this error 1017.
Solution, if you have declare:
let headers = ["":""]
the best one option is set to nil:
let headers = nil
Then you can:
sessionManager.request( "www.example.com", method: .post, parameters: parameters, encoding: encoding, headers: headers ).responseJSON { response in....
Upvotes: 0
Reputation: 1725
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.
Upvotes: 1
Reputation: 8121
This error occurs when you perform a GET
request with the body set (setHTTPBody
)
Upvotes: 4
Reputation: 41
PLease check this link
Alamofire.request(method, "(URLString)" , parameters: parameters, headers: headers).responseJSON { response in }
in post method you also add
parameters:parameters, encoding: .JSON
(if you added encoding line to GET POST then error will come "cannot parse response")
2nd please check the header as well.
["authentication_token" : "sdas3tgfghret6grfgher4y"]
then Solved this issue.
Upvotes: 0
Reputation: 852
For any other people who got error code -1017
-- I fixed it by manually setting my http headers in my http request. I think the server was having problems parsing the HTTP headers because instead of the string "Authorization" : "qii2nl32j2l3jel"
my headers didn't have the quotes like this: Authorization : "1i2j12j"
. Good luck.
Something like this:
NSMutableDictionary* newRequestHTTPHeader = [[NSMutableDictionary alloc] init];
[newRequestHTTPHeader setValue:authValue forKey:@"\"Authorization\""];
[newRequestHTTPHeader setValue:contentLengthVal forKey:@"\"Content-Length\""];
[newRequestHTTPHeader setValue:contentMD5Val forKey:@"\"Content-MD5\""];
[newRequestHTTPHeader setValue:contentTypeVal forKey:@"\"Content-Type\""];
[newRequestHTTPHeader setValue:dateVal forKey:@"\"Date\""];
[newRequestHTTPHeader setValue:hostVal forKey:@"\"Host\""];
[newRequestHTTPHeader setValue:publicValue forKey:@"\"public-read-write\""];
//the proper request is built with the new http headers.
NSMutableURLRequest* request2 = [[NSMutableURLRequest alloc] initWithURL:request.URL];
[request2 setAllHTTPHeaderFields:newRequestHTTPHeader];
[request2 setHTTPMethod:request.HTTPMethod];
Upvotes: 0
Reputation: 91
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.
Upvotes: 6
Reputation: 9354
You have something wrong with your JSON parameters:
kCFURLErrorCannotParseResponse = -1017
Upvotes: 5