I'm sending data between and iOS app and a web server with an auth token system. The token was generated through a login API with JWT and then stored locally on the device. When I use it later in a loading data request I'm getting a -1011 error with status code 401. Here's my code:
NSURL *baseURL = [NSURL URLWithString:kBaseURLString];
NSDictionary *parameters = @{@"token": authtoken};
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:@"load_data.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
[self doSomeStuff:responseObject];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Unable to load data"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
If I test my php API with curl and the same auth token it works well. But with the iOS simulator I get this error:
ERROR: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x79f848f0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ad59d90> { URL: http://localhost:8888/load_data.php } { status code: 401, headers {
Connection = "Keep-Alive";
"Content-Length" = 12;
"Content-Type" = "text/html;charset=UTF-8";
Date = "Mon, 17 Aug 2015 16:03:44 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.6.2 mod_ssl/2.2.29 OpenSSL/0.9.8za DAV/2 mod_perl/2.0.8 Perl/v5.20.0";
"X-Powered-By" = "PHP/5.6.2";
} }, NSErrorFailingURLKey=http://localhost:8888/load_data.php, NSLocalizedDescription=Request failed: unauthorized (401), com.alamofire.serialization.response.error.data=<556e6175 74686f72 697a6564>}
I'm getting the same error using a GET method. But when I copy/paste the url from the error message of the console to my browser it works like a charm. I don't understand why the AFNetworking request fails on the device.