2013-01-24 46 views
7

Tôi đang cố gắng thực hiện một yêu cầu POST đơn giản với Form-Data tới api nhưng tôi luôn nhận được trạng thái 401.AFNetworking Post Form-Data

Tôi có thể dễ dàng thực hiện cuộc gọi bên ngoài xcode (trong Postman chẳng hạn) thành công, tất cả những gì tôi làm là POST vào url với cặp khóa-giá trị kiểu Form-Data nhưng trong xcode nó luôn thất bại.

Đây là AFHTTPClient tôi:

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

Tôi cũng đã cố gắng tạo ra một yêu cầu với một 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; 
} 

Nhưng tôi nhận được cùng một lỗi chính xác tất cả các thời gian: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401". Tôi đang chờ phản hồi JSON, bạn có thể thấy điều kiện thành công trong ảnh đính kèm. Tôi nghi ngờ nó là một cái gì đó rất đơn giản nhưng tôi kéo tóc của tôi ra cố gắng để tìm thấy nó, tôi là khá mới để kết nối mạng. Bất kỳ trợ giúp sẽ là tuyệt vời, cảm ơn. Postman Success

CẬP NHẬT

Kể từ khi yêu cầu Postman có params trống và các tiêu đề, tôi figured tôi cần để có được cơ thể mẫu. Thay đổi yêu cầu:

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

Bây giờ mang lại cho tôi một lỗi 404, đó là những gì tôi mong đợi mà không có một email và mật khẩu hợp lệ, tôi chỉ cần phải tìm ra cách để thêm một đến cơ thể của hình thức, bất cứ điều gì Tôi cố gắng thêm cho tôi lỗi "yêu cầu luồng cơ thể cạn kiệt".

+0

bạn đã thêm vào giao thức để url của bạn, ví dụ http: //? –

+0

Có, URL lỗi được báo cáo là URL đúng mà tôi nên xác thực. – hokiewalrus

+1

Có thể có sự cố với xác thực http cơ bản. Đối với xác thực cơ bản, điều này làm việc cho tôi: NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @ ""]]; // tmpBase 64 là base64 thông tin đăng nhập trong định dạng tên người dùng: mật khẩu NSString * authValue = [NSString stringWithFormat: @ "Basic% @", tmpBase64]; [yêu cầu setValue: authValue forHTTPHeaderField: @ "Authorization"]; –

Trả lời

8

Got nó, hóa ra nó một chút dài dòng thêm cặp khóa/giá trị để tạo thành dữ liệu nhưng tôi thực hiện nó như vậy:

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"]; 
Các vấn đề liên quan