2013-01-24 64 views
7

我想做一個簡單的POST請求與表單數據到api,但我總是得到狀態401作爲回報。AFNetworking張貼表格數據

我可以很容易地在xcode之外(例如在Postman中)調用成功,我所做的只是使用類型爲Form-Data的鍵值對的POST進行POST,但在xcode中它總是失敗。

這裏是我的AFHTTPClient:

@implementation UnpaktAPIClient 

+ (id)sharedInstance { 
    static UnpaktAPIClient *__sharedInstance; 
    static dispatch_once_t onceToken; 
    dispatch_once(&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; 
} 

我也試圖創建一個帶有requestOperation的請求:

- (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; 
} 

但我得到確切同樣的錯誤,所有的時間:Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401"。我期待JSON反饋,您可以在附圖中看到成功條件。我懷疑這是非常簡單的事情,但我把我的頭髮拉出來試圖找到它,我對網絡很陌生。任何幫助都會很棒,謝謝。 Postman Success

UPDATE

由於郵差請求有空白PARAMS和標題,我想我需要去的形式身體。更改請求:

- (NSDictionary *)login:(NSDictionary *)credentials { 

    NSURLRequest *request = [self multipartFormRequestWithMethod:@"POST" 
                  path:@"/api/v1/users/login" 
                 parameters:nil 
             constructingBodyWithBlock:^(id<AFMultipartFormData> 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; 
} 

現在給我一個404錯誤,這正是我所期望的沒有一個有效的電子郵件和密碼,我只需要弄清楚如何一個添加到表單中,任何的身體我嘗試添加給我的「請求身體流枯竭」錯誤。

+0

你添加協議的URL,比如http://? –

+0

是的,獲得報告的錯誤URL是我應該進行身份驗證的錯誤URL。 – hokiewalrus

+1

這可能是基本http認證的問題。對於基本認證,這適用於我:NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@「」]]; // tmpBase 64是格式爲username的密碼的base64:password NSString * authValue = [NSString stringWithFormat:@「Basic%@」,tmpBase64]; [request setValue:authValue forHTTPHeaderField:@「Authorization」]; –

回答

8

明白了,原來這是一個有點冗長添加鍵/值對,以形成數據,但我做到了,像這樣:

NSMutableData *email = [[NSMutableData alloc] init]; 
NSMutableData *password = [[NSMutableData alloc] init]; 
[email appendData:[[NSString stringWithFormat:@"[email protected]"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[password appendData:[[NSString stringWithFormat:@"qwerty"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[formData appendPartWithFormData:email name:@"email"]; 
[formData appendPartWithFormData:password name:@"password"];