title: 401 error - Basic Authentication with AFNetworking AFHTTPClient & Tastypie
body: <p>I'm using AFHTTPClient from AFNetworking to make a call from my IOS app to my server, which is using Django with TastyPie. It's working great when I turn authentication off on the server side; however, when I require authentication and insert the proper username and password into my code, the I receive the following 401 authentication error:</p>
  
  <pre><code>\2012-09-16 00:24:37.877 RESTtest[76909:f803] 
  Complex AF: Error Domain=AFNetworkingErrorDomain Code=-1011 
  "Expected status code in (200-299), got 401" 
  UserInfo=0x686ba00 {AFNetworkingOperationFailingURLResponseErrorKey=&lt;NSHTTPURLResponse: 0x686f130&gt;, 
  NSErrorFailingURLKey=http://127.0.0.1:8000/api/v1/shoppinglist, 
  NSLocalizedDescription=Expected status code in (200-299), got 401, 
  AFNetworkingOperationFailingURLRequestErrorKey=&lt;NSMutableURLRequest http://127.0.0.1:8000/api/v1/shoppinglist&gt;}
  </code></pre>
  
  <p>Here is my code:</p>
  
  <p>AFAPIClient.h
      #import "AFHTTPClient.h"</p>
  
  <pre><code>@interface AFAPIClient : AFHTTPClient
  
  -(void)setUsername:(NSString *)username andPassword:(NSString *)password;
  
  + (AFAPIClient *)sharedClient;
  
  @end
  </code></pre>
  
  <p>AFAPIClient.m:</p>
  
  <pre><code>#import "AFAPIClient.h"
  #import "AFJSONRequestOperation.h"
  
  
  static NSString * const baseURL = @"http://@127.0.0.1:8000/api/v1";
  
  
  @implementation AFAPIClient
  
  + (AFAPIClient *)sharedClient {
      static AFAPIClient *_sharedClient = nil;
      static dispatch_once_t pred;
      dispatch_once(&amp;pred, ^{
          _sharedClient = [[AFAPIClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
          //[_sharedClient setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"]; I tried putting the authorization command here
      });
  
      return _sharedClient;
  };
  
  #pragma mark - Methods
  
  -(void)setUsername:(NSString *)username andPassword:(NSString *)password;
  {
      [self clearAuthorizationHeader];
      [self setAuthorizationHeaderWithUsername:username password:password];
  }
  
  - (id)initWithBaseURL:(NSURL *)url
  {
      self = [super initWithBaseURL:url];
      if (!self) {
          return nil;
      }
  
      [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
      [self setParameterEncoding:AFJSONParameterEncoding];
  
  
      //[self setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"]; I also tried putting the authorization command here
  
      // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
      [self setDefaultHeader:@"Accept" value:@"application/json"];
  
      return self;
  }
  
  
  @end
  </code></pre>
  
  <p>TQViewController.h:</p>
  
  <pre><code>[...]
  - (IBAction)sendAFClientRequest:(id)sender {
      //[[AFAPIClient sharedClient] setUsername:@"myusername" andPassword:@"mypassword"];
      [[AFAPIClient sharedClient] getPath:@"shoppinglist" parameters:nil success:^(AFHTTPRequestOperation *operation, id response) {
          NSLog(@"Complex AF: %@", [response valueForKeyPath:@"objects"]);
      } failure:^(AFHTTPRequestOperation *operation, id response) {
          NSLog(@"Complex AF: %@", response);
      }
       ];
  
  }
  [...]
  </code></pre>
  
  <p>I know this isn't a problem with my server or my username/password, as I can authenticate just fine by inserting the username/password into the URL:</p>
  
  <pre><code>@"http://myusername:mypassword@127.0.0.1:8000/api/v1/shoppinglist"
  </code></pre>
  
  <p>Any help on this would be great. It would be wonderful to be able to use AFHTTPClient without inserting the authentication information directly into the static base URL, which seems completely improper. Thanks in advance!</p>