title: AFNetworking Post Form-Data
body: <p>I am trying to do a simple POST request with Form-Data to an api but I always get a status 401 in return.</p>
  
  <p>I can easily make the call outside of xcode (in Postman for instance) with success, all I do is POST to the url with key-value pairs of type Form-Data but in xcode it always fails.</p>
  
  <p>Here is my AFHTTPClient:</p>
  
  <pre><code>@implementation UnpaktAPIClient
  
  + (id)sharedInstance {
      static UnpaktAPIClient *__sharedInstance;
      static dispatch_once_t onceToken;
      dispatch_once(&amp;onceToken, ^{
          __sharedInstance = [[UnpaktAPIClient alloc] initWithBaseURL:[NSURL URLWithString:UnpaktAPIBaseURLString]];
      });
      return __sharedInstance;
  }
  
  - (id)initWithBaseURL:(NSURL *)url {
      self = [super initWithBaseURL:url];
      if (self) {
          [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
          [self setParameterEncoding:AFFormURLParameterEncoding];
      }
      return self;
  }
  
  - (NSDictionary *)login:(NSDictionary *)credentials {
  
      [[UnpaktAPIClient sharedInstance] postPath:@"/api/v1/users/login"
                                      parameters:credentials
                                          success:^(AFHTTPRequestOperation *operation, id response) {
                                              NSLog(@"success");
                                          }
                                          failure:^(AFHTTPRequestOperation *operation, NSError *error){
                                              NSLog(@"%@", error);
                                          }];
  
      return nil;
  }
  </code></pre>
  
  <p>I've also attempted creating a request with a requestOperation:</p>
  
  <pre><code>- (NSDictionary *)login:(NSDictionary *)credentials {
  
      NSMutableURLRequest *request = [[UnpaktAPIClient sharedInstance] requestWithMethod:@"POST" path:@"/api/v1/users/login" parameters:credentials];
  
      AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                          success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                                                                                              NSLog(@"success");
                                                                                          }
                                                                                          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id json){
                                                                                              NSLog(@"%@", error);
                                                                                          }];
      [operation start];
  
      return nil;
  }
  </code></pre>
  
  <p>But I get the exact same error all the time: <code>Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401"</code>. I'm expecting JSON feedback, you can see the success condition in the attached picture. I suspect it's something very simple but I'm pulling my hair out trying to find it, I'm quite new to networking. Any help would be awesome, thanks.<img src="https://i.stack.imgur.com/6czmq.png" alt="Postman Success"></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Since the Postman request has blank params and headers, I figured I needed to get to the form body. Changing the request to:</p>
  
  <pre><code>- (NSDictionary *)login:(NSDictionary *)credentials {
  
      NSURLRequest *request = [self multipartFormRequestWithMethod:@"POST"
                                                              path:@"/api/v1/users/login"
                                                        parameters:nil
                                         constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt; formData) {
  
                                         }];
      AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                          success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                                                                                              NSLog(@"success");
                                                                                          }
                                                                                          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id json) {
                                                                                              NSLog(@"%@", error);
                                                                                          }];
      [operation start];
      return nil;
  }
  </code></pre>
  
  <p>Now gives me a 404 error, which is what I would expect without a valid email and password, I just need to figure out how to add one to the body of the form, anything I try to add gives me the "request body stream exhausted" error.</p>