I am trying to make a webservice call in which i am sending some input data and getting cookies. for the subsequent calls, i am using cookies which i got from the last call and make a new call. But unfortunately i got 401 error. But when i try this in another system i got a response.
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo=0xb469a60 {NSErrorFailingURLKey=https://<base url>/<tenant>/<resource>, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb550b60> { URL: https://<base url>/<tenant>/<resource> } { status code: 401, headers {
"Cache-Control" = private;
"Content-Length" = 0;
Date = "Wed, 04 Dec 2013 08:55:47 GMT";
Server = "Microsoft-HTTPAPI/2.0";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: unauthorized (401)}
This is my post request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
serializer.acceptableContentTypes = @"application/json";
manager.responseSerializer = serializer;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSURL* networkAddress = [NSURL URLWithString:baseServerURL];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:networkAddress];
if (cookies.count > 0)
{
[self saveCookies];
success(responseObject);
}
else
{
failure([NSError errorWithDomain:@"App" code:1 userInfo:@{@"error": @"An Error Occured"}]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self clearCookies];
failure (error);
}];
I tried resetting my simulator and repened my xcode. but nothing seems to be help. But in another system i am able to get all data.
PS: i am making this call after login. so unauthorized access seems to be invalid. i think it is a problem from client side.
Any help is appreciated,
I'm having the exact same issue, the only way I've found that works is calling your request inside the success block of your request to login. It sucks but I've spent hours trying everything I can to get it to work. Will update if I find out how.
[[AFAppDotNetAPIClient sharedClient] POST:@"users/login" parameters:loginDetails success:^(NSURLSessionDataTask * __unused task, id JSON) {
[[AFAppDotNetAPIClient sharedClient] POST:@"posts" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
} failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
}];
} failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
}];
UPDATE: Yeah the emulator kind of doesn't work.... tested on a device and was fine.
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