Trouble getting a 401 response (unauthorized) from an iPhone app?

I'm more of a Rails guy than an iPhone guy, but quick googling (see thread here: discussions.apple.com/thread.jspa?messag... ) that NSURLConnection only returns a NSHTTPURLResponse when it's successful (200) and otherwise returns the null response and the behavior you're seeing on a 401.

Up vote 2 down vote favorite share g+ share fb share tw.

I have a synchronous method to send out requests that looks like the following: + (NSString *)sendRequest:(NSMutableURLRequest *)request { NSHTTPURLResponse *response; NSError *error; NSData *responseData = NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error; NSLog(@"response = %@", response); NSLog(@"error = %@", error localizedDescription); NSString *responseString = NSString alloc initWithData:responseData encoding:NSUTF8StringEncoding; responseString autorelease; NSLog(@"%@: %@", request HTTPMethod, request URL); NSLog(@"Response Code: %d", response statusCode); NSLog(@"Content-Type: %@", response allHeaderFields objectForKey:@"Content-Type"); NSLog(@"Response: %@\n\n", responseString); return responseString; } For a particular request, I am getting a null response with an error saying "The operation couldn’t be completed. (NSURLErrorDomain error -1012. )" According to the Foundation constants reference this code is NSURLErrorUserCancelledAuthentication which is described as Returned when an asynchronous request for authentication is cancelled by the user.

This is typically incurred by clicking a "Cancel" button in a username/password dialog, rather than the user making an attempt to authenticate. However, I there is no user clicking cancel. I am simply making a request to retrieve a list of events without being authorized and therefore the 401 I return should produce an unauthorized response, not a null response with a NSURLErrorUserCancelledAuthentication error.

My Rails code that returns this response is the following: def require_auth # Return unauthorized if the API call was not made by an authorized user unless current_user respond_to do |format| #format. Json { render :text => "Unauthorized", :status => :unauthorized } format. Json { head :unauthorized } format.

Xml { head :unauthorized } end end end and the output from my Rails log looks like this: Started GET "/events. Json" for 127.0.0.1 at Fri Jan 07 10:51:47 -0500 2011 Processing by EventsController#index as JSON Completed 401 Unauthorized in 0ms Does anyone know why this is happening and how to create the correct response? Iphone ruby-on-rails unauthorized nsurlerrordomain link|improve this question asked Jan 7 '11 at 16:16Tony3,43634187 74% accept rate.

I'm more of a Rails guy than an iPhone guy, but quick googling (see thread here: discussions.apple.com/thread.jspa?messag...) that NSURLConnection only returns a NSHTTPURLResponse when it's successful (200) and otherwise returns the null response and the behavior you're seeing on a 401. It looks like this is the nature of the API and you'll need to use something lower level if you want to distinguish between different non-200 status responses. This other Stackoverflow thread seems to imply the asynchronous version of the API will let you get all of the response codes and data: iOS: How can I receive HTTP 401 instead of -1012 NSURLErrorUserCancelledAuthentication.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions